summaryrefslogtreecommitdiff
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.cpp37
1 files changed, 20 insertions, 17 deletions
diff --git a/src/core/crypto/aes_util.cpp b/src/core/crypto/aes_util.cpp
index 89ade5000..4be76bb43 100644
--- a/src/core/crypto/aes_util.cpp
+++ b/src/core/crypto/aes_util.cpp
@@ -10,9 +10,9 @@
10 10
11namespace Core::Crypto { 11namespace Core::Crypto {
12namespace { 12namespace {
13std::vector<u8> CalculateNintendoTweak(size_t sector_id) { 13std::vector<u8> CalculateNintendoTweak(std::size_t sector_id) {
14 std::vector<u8> out(0x10); 14 std::vector<u8> out(0x10);
15 for (size_t i = 0xF; i <= 0xF; --i) { 15 for (std::size_t i = 0xF; i <= 0xF; --i) {
16 out[i] = sector_id & 0xFF; 16 out[i] = sector_id & 0xFF;
17 sector_id >>= 8; 17 sector_id >>= 8;
18 } 18 }
@@ -20,11 +20,14 @@ std::vector<u8> CalculateNintendoTweak(size_t sector_id) {
20} 20}
21} // Anonymous namespace 21} // Anonymous namespace
22 22
23static_assert(static_cast<size_t>(Mode::CTR) == static_cast<size_t>(MBEDTLS_CIPHER_AES_128_CTR), 23static_assert(static_cast<std::size_t>(Mode::CTR) ==
24 static_cast<std::size_t>(MBEDTLS_CIPHER_AES_128_CTR),
24 "CTR has incorrect value."); 25 "CTR has incorrect value.");
25static_assert(static_cast<size_t>(Mode::ECB) == static_cast<size_t>(MBEDTLS_CIPHER_AES_128_ECB), 26static_assert(static_cast<std::size_t>(Mode::ECB) ==
27 static_cast<std::size_t>(MBEDTLS_CIPHER_AES_128_ECB),
26 "ECB has incorrect value."); 28 "ECB has incorrect value.");
27static_assert(static_cast<size_t>(Mode::XTS) == static_cast<size_t>(MBEDTLS_CIPHER_AES_128_XTS), 29static_assert(static_cast<std::size_t>(Mode::XTS) ==
30 static_cast<std::size_t>(MBEDTLS_CIPHER_AES_128_XTS),
28 "XTS has incorrect value."); 31 "XTS has incorrect value.");
29 32
30// Structure to hide mbedtls types from header file 33// Structure to hide mbedtls types from header file
@@ -33,7 +36,7 @@ struct CipherContext {
33 mbedtls_cipher_context_t decryption_context; 36 mbedtls_cipher_context_t decryption_context;
34}; 37};
35 38
36template <typename Key, size_t KeySize> 39template <typename Key, std::size_t KeySize>
37Crypto::AESCipher<Key, KeySize>::AESCipher(Key key, Mode mode) 40Crypto::AESCipher<Key, KeySize>::AESCipher(Key key, Mode mode)
38 : ctx(std::make_unique<CipherContext>()) { 41 : ctx(std::make_unique<CipherContext>()) {
39 mbedtls_cipher_init(&ctx->encryption_context); 42 mbedtls_cipher_init(&ctx->encryption_context);
@@ -54,26 +57,26 @@ Crypto::AESCipher<Key, KeySize>::AESCipher(Key key, Mode mode)
54 //"Failed to set key on mbedtls ciphers."); 57 //"Failed to set key on mbedtls ciphers.");
55} 58}
56 59
57template <typename Key, size_t KeySize> 60template <typename Key, std::size_t KeySize>
58AESCipher<Key, KeySize>::~AESCipher() { 61AESCipher<Key, KeySize>::~AESCipher() {
59 mbedtls_cipher_free(&ctx->encryption_context); 62 mbedtls_cipher_free(&ctx->encryption_context);
60 mbedtls_cipher_free(&ctx->decryption_context); 63 mbedtls_cipher_free(&ctx->decryption_context);
61} 64}
62 65
63template <typename Key, size_t KeySize> 66template <typename Key, std::size_t KeySize>
64void AESCipher<Key, KeySize>::SetIV(std::vector<u8> iv) { 67void AESCipher<Key, KeySize>::SetIV(std::vector<u8> iv) {
65 ASSERT_MSG((mbedtls_cipher_set_iv(&ctx->encryption_context, iv.data(), iv.size()) || 68 ASSERT_MSG((mbedtls_cipher_set_iv(&ctx->encryption_context, iv.data(), iv.size()) ||
66 mbedtls_cipher_set_iv(&ctx->decryption_context, iv.data(), iv.size())) == 0, 69 mbedtls_cipher_set_iv(&ctx->decryption_context, iv.data(), iv.size())) == 0,
67 "Failed to set IV on mbedtls ciphers."); 70 "Failed to set IV on mbedtls ciphers.");
68} 71}
69 72
70template <typename Key, size_t KeySize> 73template <typename Key, std::size_t KeySize>
71void AESCipher<Key, KeySize>::Transcode(const u8* src, size_t size, u8* dest, Op op) const { 74void AESCipher<Key, KeySize>::Transcode(const u8* src, std::size_t size, u8* dest, Op op) const {
72 auto* const context = op == Op::Encrypt ? &ctx->encryption_context : &ctx->decryption_context; 75 auto* const context = op == Op::Encrypt ? &ctx->encryption_context : &ctx->decryption_context;
73 76
74 mbedtls_cipher_reset(context); 77 mbedtls_cipher_reset(context);
75 78
76 size_t written = 0; 79 std::size_t written = 0;
77 if (mbedtls_cipher_get_cipher_mode(context) == MBEDTLS_MODE_XTS) { 80 if (mbedtls_cipher_get_cipher_mode(context) == MBEDTLS_MODE_XTS) {
78 mbedtls_cipher_update(context, src, size, dest, &written); 81 mbedtls_cipher_update(context, src, size, dest, &written);
79 if (written != size) { 82 if (written != size) {
@@ -90,8 +93,8 @@ void AESCipher<Key, KeySize>::Transcode(const u8* src, size_t size, u8* dest, Op
90 return; 93 return;
91 } 94 }
92 95
93 for (size_t offset = 0; offset < size; offset += block_size) { 96 for (std::size_t offset = 0; offset < size; offset += block_size) {
94 auto length = std::min<size_t>(block_size, size - offset); 97 auto length = std::min<std::size_t>(block_size, size - offset);
95 mbedtls_cipher_update(context, src + offset, length, dest + offset, &written); 98 mbedtls_cipher_update(context, src + offset, length, dest + offset, &written);
96 if (written != length) { 99 if (written != length) {
97 if (length < block_size) { 100 if (length < block_size) {
@@ -110,12 +113,12 @@ void AESCipher<Key, KeySize>::Transcode(const u8* src, size_t size, u8* dest, Op
110 mbedtls_cipher_finish(context, nullptr, nullptr); 113 mbedtls_cipher_finish(context, nullptr, nullptr);
111} 114}
112 115
113template <typename Key, size_t KeySize> 116template <typename Key, std::size_t KeySize>
114void AESCipher<Key, KeySize>::XTSTranscode(const u8* src, size_t size, u8* dest, size_t sector_id, 117void AESCipher<Key, KeySize>::XTSTranscode(const u8* src, std::size_t size, u8* dest,
115 size_t sector_size, Op op) { 118 std::size_t sector_id, std::size_t sector_size, Op op) {
116 ASSERT_MSG(size % sector_size == 0, "XTS decryption size must be a multiple of sector size."); 119 ASSERT_MSG(size % sector_size == 0, "XTS decryption size must be a multiple of sector size.");
117 120
118 for (size_t i = 0; i < size; i += sector_size) { 121 for (std::size_t i = 0; i < size; i += sector_size) {
119 SetIV(CalculateNintendoTweak(sector_id++)); 122 SetIV(CalculateNintendoTweak(sector_id++));
120 Transcode<u8, u8>(src + i, sector_size, dest + i, op); 123 Transcode<u8, u8>(src + i, sector_size, dest + i, op);
121 } 124 }