From df5b75694f5abde94ccf05fa6c7a557b1ba9079b Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Fri, 27 Jul 2018 23:55:23 -0400 Subject: Remove files that are not used --- src/core/crypto/aes_util.h | 118 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 src/core/crypto/aes_util.h (limited to 'src/core/crypto/aes_util.h') diff --git a/src/core/crypto/aes_util.h b/src/core/crypto/aes_util.h new file mode 100644 index 000000000..9807b9234 --- /dev/null +++ b/src/core/crypto/aes_util.h @@ -0,0 +1,118 @@ +// Copyright 2018 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "common/assert.h" +#include "core/file_sys/vfs.h" +#include "mbedtls/cipher.h" + +namespace Crypto { + +enum class Mode { + CTR = MBEDTLS_CIPHER_AES_128_CTR, + ECB = MBEDTLS_CIPHER_AES_128_ECB, + XTS = MBEDTLS_CIPHER_AES_128_XTS, +}; + +enum class Op { + ENCRYPT, + DECRYPT, +}; + +template +struct AESCipher { + static_assert(std::is_same_v>, "Key must be std::array of u8."); + static_assert(KeySize == 0x10 || KeySize == 0x20, "KeySize must be 128 or 256."); + + AESCipher(Key key, Mode mode) { + mbedtls_cipher_init(&encryption_context); + mbedtls_cipher_init(&decryption_context); + + ASSERT_MSG((mbedtls_cipher_setup( + &encryption_context, + mbedtls_cipher_info_from_type(static_cast(mode))) || + mbedtls_cipher_setup(&decryption_context, + mbedtls_cipher_info_from_type( + static_cast(mode)))) == 0, + "Failed to initialize mbedtls ciphers."); + + ASSERT( + !mbedtls_cipher_setkey(&encryption_context, key.data(), KeySize * 8, MBEDTLS_ENCRYPT)); + ASSERT( + !mbedtls_cipher_setkey(&decryption_context, key.data(), KeySize * 8, MBEDTLS_DECRYPT)); + //"Failed to set key on mbedtls ciphers."); + } + + ~AESCipher() { + mbedtls_cipher_free(&encryption_context); + mbedtls_cipher_free(&decryption_context); + } + + void SetIV(std::vector iv) { + ASSERT_MSG((mbedtls_cipher_set_iv(&encryption_context, iv.data(), iv.size()) || + mbedtls_cipher_set_iv(&decryption_context, iv.data(), iv.size())) == 0, + "Failed to set IV on mbedtls ciphers."); + } + + template + void Transcode(const Source* src, size_t size, Dest* dest, Op op) { + size_t written = 0; + + const auto context = op == Op::ENCRYPT ? &encryption_context : &decryption_context; + + mbedtls_cipher_reset(context); + + if (mbedtls_cipher_get_cipher_mode(context) == MBEDTLS_MODE_XTS) { + mbedtls_cipher_update(context, reinterpret_cast(src), size, + reinterpret_cast(dest), &written); + 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(block_size, size - offset); + mbedtls_cipher_update(context, reinterpret_cast(src) + offset, length, + reinterpret_cast(dest) + offset, &written); + if (written != length) + LOG_WARNING(Crypto, + "Not all data was decrypted requested={:016X}, actual={:016X}.", + length, written); + } + } + + mbedtls_cipher_finish(context, nullptr, nullptr); + } + + template + void XTSTranscode(const Source* src, size_t size, Dest* dest, size_t sector_id, + size_t sector_size, Op op) { + if (size % sector_size > 0) { + LOG_CRITICAL(Crypto, "Data size must be a multiple of sector size."); + return; + } + + for (size_t i = 0; i < size; i += sector_size) { + SetIV(CalculateNintendoTweak(sector_id++)); + Transcode(reinterpret_cast(src) + i, sector_size, + reinterpret_cast(dest) + i, op); + } + } + +private: + mbedtls_cipher_context_t encryption_context; + mbedtls_cipher_context_t decryption_context; + + static std::vector CalculateNintendoTweak(size_t sector_id) { + std::vector out(0x10); + for (size_t i = 0xF; i <= 0xF; --i) { + out[i] = sector_id & 0xFF; + sector_id >>= 8; + } + return out; + } +}; +} // namespace Crypto -- cgit v1.2.3 From 22342487e8fb851a9837db22408db56240aa6931 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sat, 28 Jul 2018 16:23:00 -0400 Subject: Extract mbedtls to cpp file --- src/core/crypto/aes_util.h | 106 ++++++++++----------------------------------- 1 file changed, 23 insertions(+), 83 deletions(-) (limited to 'src/core/crypto/aes_util.h') diff --git a/src/core/crypto/aes_util.h b/src/core/crypto/aes_util.h index 9807b9234..5c09718b2 100644 --- a/src/core/crypto/aes_util.h +++ b/src/core/crypto/aes_util.h @@ -6,113 +6,53 @@ #include "common/assert.h" #include "core/file_sys/vfs.h" -#include "mbedtls/cipher.h" -namespace Crypto { +namespace Core::Crypto { enum class Mode { - CTR = MBEDTLS_CIPHER_AES_128_CTR, - ECB = MBEDTLS_CIPHER_AES_128_ECB, - XTS = MBEDTLS_CIPHER_AES_128_XTS, + CTR = 11, + ECB = 2, + XTS = 70, }; enum class Op { - ENCRYPT, - DECRYPT, + Encrypt, + Decrypt, }; +struct mbedtls_cipher_context_t; + template -struct AESCipher { +class AESCipher { static_assert(std::is_same_v>, "Key must be std::array of u8."); static_assert(KeySize == 0x10 || KeySize == 0x20, "KeySize must be 128 or 256."); - AESCipher(Key key, Mode mode) { - mbedtls_cipher_init(&encryption_context); - mbedtls_cipher_init(&decryption_context); - - ASSERT_MSG((mbedtls_cipher_setup( - &encryption_context, - mbedtls_cipher_info_from_type(static_cast(mode))) || - mbedtls_cipher_setup(&decryption_context, - mbedtls_cipher_info_from_type( - static_cast(mode)))) == 0, - "Failed to initialize mbedtls ciphers."); - - ASSERT( - !mbedtls_cipher_setkey(&encryption_context, key.data(), KeySize * 8, MBEDTLS_ENCRYPT)); - ASSERT( - !mbedtls_cipher_setkey(&decryption_context, key.data(), KeySize * 8, MBEDTLS_DECRYPT)); - //"Failed to set key on mbedtls ciphers."); - } +public: + AESCipher(Key key, Mode mode); - ~AESCipher() { - mbedtls_cipher_free(&encryption_context); - mbedtls_cipher_free(&decryption_context); - } + ~AESCipher(); - void SetIV(std::vector iv) { - ASSERT_MSG((mbedtls_cipher_set_iv(&encryption_context, iv.data(), iv.size()) || - mbedtls_cipher_set_iv(&decryption_context, iv.data(), iv.size())) == 0, - "Failed to set IV on mbedtls ciphers."); - } + void SetIV(std::vector iv); template void Transcode(const Source* src, size_t size, Dest* dest, Op op) { - size_t written = 0; - - const auto context = op == Op::ENCRYPT ? &encryption_context : &decryption_context; - - mbedtls_cipher_reset(context); - - if (mbedtls_cipher_get_cipher_mode(context) == MBEDTLS_MODE_XTS) { - mbedtls_cipher_update(context, reinterpret_cast(src), size, - reinterpret_cast(dest), &written); - 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(block_size, size - offset); - mbedtls_cipher_update(context, reinterpret_cast(src) + offset, length, - reinterpret_cast(dest) + offset, &written); - if (written != length) - LOG_WARNING(Crypto, - "Not all data was decrypted requested={:016X}, actual={:016X}.", - length, written); - } - } - - mbedtls_cipher_finish(context, nullptr, nullptr); + Transcode(reinterpret_cast(src), size, reinterpret_cast(dest), op); } + void Transcode(const u8* src, size_t size, u8* dest, Op op); + template void XTSTranscode(const Source* src, size_t size, Dest* dest, size_t sector_id, size_t sector_size, Op op) { - if (size % sector_size > 0) { - LOG_CRITICAL(Crypto, "Data size must be a multiple of sector size."); - return; - } - - for (size_t i = 0; i < size; i += sector_size) { - SetIV(CalculateNintendoTweak(sector_id++)); - Transcode(reinterpret_cast(src) + i, sector_size, - reinterpret_cast(dest) + i, op); - } + XTSTranscode(reinterpret_cast(src), size, reinterpret_cast(dest), sector_id, sector_size, op); } + void XTSTranscode(const u8* src, size_t size, u8* dest, size_t sector_id, size_t sector_size, Op op); + private: - mbedtls_cipher_context_t encryption_context; - mbedtls_cipher_context_t decryption_context; - - static std::vector CalculateNintendoTweak(size_t sector_id) { - std::vector out(0x10); - for (size_t i = 0xF; i <= 0xF; --i) { - out[i] = sector_id & 0xFF; - sector_id >>= 8; - } - return out; - } + std::unique_ptr encryption_context; + std::unique_ptr decryption_context; + + static std::vector CalculateNintendoTweak(size_t sector_id); }; } // namespace Crypto -- cgit v1.2.3 From 239a3113e4c6a53a2c7b12e67a0f21afae24b0aa Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sat, 28 Jul 2018 21:39:42 -0400 Subject: Make XCI comply to review and style guidelines --- src/core/crypto/aes_util.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'src/core/crypto/aes_util.h') diff --git a/src/core/crypto/aes_util.h b/src/core/crypto/aes_util.h index 5c09718b2..fa77d5560 100644 --- a/src/core/crypto/aes_util.h +++ b/src/core/crypto/aes_util.h @@ -20,7 +20,7 @@ enum class Op { Decrypt, }; -struct mbedtls_cipher_context_t; +struct CipherContext; template class AESCipher { @@ -44,15 +44,16 @@ public: template void XTSTranscode(const Source* src, size_t size, Dest* dest, size_t sector_id, size_t sector_size, Op op) { - XTSTranscode(reinterpret_cast(src), size, reinterpret_cast(dest), sector_id, sector_size, op); + XTSTranscode(reinterpret_cast(src), size, reinterpret_cast(dest), sector_id, + sector_size, op); } - void XTSTranscode(const u8* src, size_t size, u8* dest, size_t sector_id, size_t sector_size, Op op); + void XTSTranscode(const u8* src, size_t size, u8* dest, size_t sector_id, size_t sector_size, + Op op); private: - std::unique_ptr encryption_context; - std::unique_ptr decryption_context; + std::unique_ptr ctx; static std::vector CalculateNintendoTweak(size_t sector_id); }; -} // namespace Crypto +} // namespace Core::Crypto -- cgit v1.2.3 From 03149d3e4a7f8038d9c88cbeb19dee25a39e0042 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sun, 29 Jul 2018 19:00:09 -0400 Subject: Add missing includes and use const where applicable --- src/core/crypto/aes_util.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/core/crypto/aes_util.h') diff --git a/src/core/crypto/aes_util.h b/src/core/crypto/aes_util.h index fa77d5560..5b0b02738 100644 --- a/src/core/crypto/aes_util.h +++ b/src/core/crypto/aes_util.h @@ -4,11 +4,16 @@ #pragma once +#include +#include +#include #include "common/assert.h" #include "core/file_sys/vfs.h" namespace Core::Crypto { +struct CipherContext; + enum class Mode { CTR = 11, ECB = 2, @@ -20,8 +25,6 @@ enum class Op { Decrypt, }; -struct CipherContext; - template class AESCipher { static_assert(std::is_same_v>, "Key must be std::array of u8."); -- cgit v1.2.3