diff options
| author | 2018-07-28 16:23:00 -0400 | |
|---|---|---|
| committer | 2018-08-01 00:16:54 -0400 | |
| commit | 22342487e8fb851a9837db22408db56240aa6931 (patch) | |
| tree | 65d8ec8cd180656e4ec6bb2ac5c928712f43122a /src/core/crypto/aes_util.cpp | |
| parent | Add missing string.h include (diff) | |
| download | yuzu-22342487e8fb851a9837db22408db56240aa6931.tar.gz yuzu-22342487e8fb851a9837db22408db56240aa6931.tar.xz yuzu-22342487e8fb851a9837db22408db56240aa6931.zip | |
Extract mbedtls to cpp file
Diffstat (limited to 'src/core/crypto/aes_util.cpp')
| -rw-r--r-- | src/core/crypto/aes_util.cpp | 102 |
1 files changed, 100 insertions, 2 deletions
diff --git a/src/core/crypto/aes_util.cpp b/src/core/crypto/aes_util.cpp index 46326cdec..a9646e52f 100644 --- a/src/core/crypto/aes_util.cpp +++ b/src/core/crypto/aes_util.cpp | |||
| @@ -2,5 +2,103 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | namespace Crypto { | 5 | #include "core/crypto/aes_util.h" |
| 6 | } // namespace Crypto | 6 | #include "mbedtls/cipher.h" |
| 7 | |||
| 8 | namespace Core::Crypto { | ||
| 9 | static_assert(static_cast<size_t>(Mode::CTR) == static_cast<size_t>(MBEDTLS_CIPHER_AES_128_CTR), "CTR mode is incorrect."); | ||
| 10 | static_assert(static_cast<size_t>(Mode::ECB) == static_cast<size_t>(MBEDTLS_CIPHER_AES_128_ECB), "ECB mode is incorrect."); | ||
| 11 | static_assert(static_cast<size_t>(Mode::XTS) == static_cast<size_t>(MBEDTLS_CIPHER_AES_128_XTS), "XTS mode is incorrect."); | ||
| 12 | |||
| 13 | template<typename Key, size_t KeySize> | ||
| 14 | Crypto::AESCipher<Key, KeySize>::AESCipher(Key key, Mode mode) { | ||
| 15 | mbedtls_cipher_init(encryption_context.get()); | ||
| 16 | mbedtls_cipher_init(decryption_context.get()); | ||
| 17 | |||
| 18 | ASSERT_MSG((mbedtls_cipher_setup( | ||
| 19 | encryption_context.get(), | ||
| 20 | mbedtls_cipher_info_from_type(static_cast<mbedtls_cipher_type_t>(mode))) || | ||
| 21 | mbedtls_cipher_setup(decryption_context.get(), | ||
| 22 | mbedtls_cipher_info_from_type( | ||
| 23 | static_cast<mbedtls_cipher_type_t>(mode)))) == 0, | ||
| 24 | "Failed to initialize mbedtls ciphers."); | ||
| 25 | |||
| 26 | ASSERT( | ||
| 27 | !mbedtls_cipher_setkey(encryption_context.get(), key.data(), KeySize * 8, MBEDTLS_ENCRYPT)); | ||
| 28 | ASSERT( | ||
| 29 | !mbedtls_cipher_setkey(decryption_context.get(), key.data(), KeySize * 8, MBEDTLS_DECRYPT)); | ||
| 30 | //"Failed to set key on mbedtls ciphers."); | ||
| 31 | } | ||
| 32 | |||
| 33 | template<typename Key, size_t KeySize> | ||
| 34 | AESCipher<Key, KeySize>::~AESCipher() { | ||
| 35 | mbedtls_cipher_free(encryption_context.get()); | ||
| 36 | mbedtls_cipher_free(decryption_context.get()); | ||
| 37 | } | ||
| 38 | |||
| 39 | template<typename Key, size_t KeySize> | ||
| 40 | void AESCipher<Key, KeySize>::SetIV(std::vector<u8> iv) { | ||
| 41 | ASSERT_MSG((mbedtls_cipher_set_iv(encryption_context.get(), iv.data(), iv.size()) || | ||
| 42 | mbedtls_cipher_set_iv(decryption_context.get(), iv.data(), iv.size())) == 0, | ||
| 43 | "Failed to set IV on mbedtls ciphers."); | ||
| 44 | } | ||
| 45 | |||
| 46 | template<typename Key, size_t KeySize> | ||
| 47 | void AESCipher<Key, KeySize>::Transcode(const u8* src, size_t size, u8* dest, Op op) { | ||
| 48 | size_t written = 0; | ||
| 49 | |||
| 50 | const auto context = op == Op::Encrypt ? encryption_context.get() : decryption_context.get(); | ||
| 51 | |||
| 52 | mbedtls_cipher_reset(context); | ||
| 53 | |||
| 54 | if (mbedtls_cipher_get_cipher_mode(context) == MBEDTLS_MODE_XTS) { | ||
| 55 | mbedtls_cipher_update(context, src, size, | ||
| 56 | dest, &written); | ||
| 57 | if (written != size) | ||
| 58 | LOG_WARNING(Crypto, "Not all data was decrypted requested={:016X}, actual={:016X}.", | ||
| 59 | size, written); | ||
| 60 | } else { | ||
| 61 | const auto block_size = mbedtls_cipher_get_block_size(context); | ||
| 62 | |||
| 63 | for (size_t offset = 0; offset < size; offset += block_size) { | ||
| 64 | auto length = std::min<size_t>(block_size, size - offset); | ||
| 65 | mbedtls_cipher_update(context, src + offset, length, | ||
| 66 | dest + offset, &written); | ||
| 67 | if (written != length) | ||
| 68 | LOG_WARNING(Crypto, | ||
| 69 | "Not all data was decrypted requested={:016X}, actual={:016X}.", | ||
| 70 | length, written); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | mbedtls_cipher_finish(context, nullptr, nullptr); | ||
| 75 | } | ||
| 76 | |||
| 77 | template<typename Key, size_t KeySize> | ||
| 78 | void AESCipher<Key, KeySize>::XTSTranscode(const u8* src, size_t size, u8* dest, size_t sector_id, size_t sector_size, | ||
| 79 | Op op) { | ||
| 80 | if (size % sector_size > 0) { | ||
| 81 | LOG_CRITICAL(Crypto, "Data size must be a multiple of sector size."); | ||
| 82 | return; | ||
| 83 | } | ||
| 84 | |||
| 85 | for (size_t i = 0; i < size; i += sector_size) { | ||
| 86 | SetIV(CalculateNintendoTweak(sector_id++)); | ||
| 87 | Transcode<u8, u8>(src + i, sector_size, | ||
| 88 | dest + i, op); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | template<typename Key, size_t KeySize> | ||
| 93 | std::vector<u8> AESCipher<Key, KeySize>::CalculateNintendoTweak(size_t sector_id) { | ||
| 94 | std::vector<u8> out(0x10); | ||
| 95 | for (size_t i = 0xF; i <= 0xF; --i) { | ||
| 96 | out[i] = sector_id & 0xFF; | ||
| 97 | sector_id >>= 8; | ||
| 98 | } | ||
| 99 | return out; | ||
| 100 | } | ||
| 101 | |||
| 102 | template class AESCipher<Key128>; | ||
| 103 | template class AESCipher<Key256>; | ||
| 104 | } \ No newline at end of file | ||