diff options
| author | 2018-08-04 14:33:11 -0400 | |
|---|---|---|
| committer | 2018-08-04 14:33:11 -0400 | |
| commit | 2b06301dbfbfe79687219bf7783a6d1b47695401 (patch) | |
| tree | 222cc27ecbc7f7e86d2edef8d36436600dee7d7a /src/core/crypto | |
| parent | Merge pull request #919 from lioncash/sign (diff) | |
| parent | Add missing parameter to files.push_back() (diff) | |
| download | yuzu-2b06301dbfbfe79687219bf7783a6d1b47695401.tar.gz yuzu-2b06301dbfbfe79687219bf7783a6d1b47695401.tar.xz yuzu-2b06301dbfbfe79687219bf7783a6d1b47695401.zip | |
Merge pull request #849 from DarkLordZach/xci
XCI and Encrypted NCA Support
Diffstat (limited to 'src/core/crypto')
| -rw-r--r-- | src/core/crypto/aes_util.cpp | 112 | ||||
| -rw-r--r-- | src/core/crypto/aes_util.h | 62 | ||||
| -rw-r--r-- | src/core/crypto/ctr_encryption_layer.cpp | 56 | ||||
| -rw-r--r-- | src/core/crypto/ctr_encryption_layer.h | 33 | ||||
| -rw-r--r-- | src/core/crypto/encryption_layer.cpp | 42 | ||||
| -rw-r--r-- | src/core/crypto/encryption_layer.h | 32 | ||||
| -rw-r--r-- | src/core/crypto/key_manager.cpp | 215 | ||||
| -rw-r--r-- | src/core/crypto/key_manager.h | 119 | ||||
| -rw-r--r-- | src/core/crypto/sha_util.cpp | 5 | ||||
| -rw-r--r-- | src/core/crypto/sha_util.h | 20 |
10 files changed, 696 insertions, 0 deletions
diff --git a/src/core/crypto/aes_util.cpp b/src/core/crypto/aes_util.cpp new file mode 100644 index 000000000..4690af5f8 --- /dev/null +++ b/src/core/crypto/aes_util.cpp | |||
| @@ -0,0 +1,112 @@ | |||
| 1 | // Copyright 2018 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <mbedtls/cipher.h> | ||
| 6 | #include "core/crypto/aes_util.h" | ||
| 7 | #include "core/crypto/key_manager.h" | ||
| 8 | |||
| 9 | namespace Core::Crypto { | ||
| 10 | |||
| 11 | static_assert(static_cast<size_t>(Mode::CTR) == static_cast<size_t>(MBEDTLS_CIPHER_AES_128_CTR), | ||
| 12 | "CTR has incorrect value."); | ||
| 13 | static_assert(static_cast<size_t>(Mode::ECB) == static_cast<size_t>(MBEDTLS_CIPHER_AES_128_ECB), | ||
| 14 | "ECB has incorrect value."); | ||
| 15 | static_assert(static_cast<size_t>(Mode::XTS) == static_cast<size_t>(MBEDTLS_CIPHER_AES_128_XTS), | ||
| 16 | "XTS has incorrect value."); | ||
| 17 | |||
| 18 | // Structure to hide mbedtls types from header file | ||
| 19 | struct CipherContext { | ||
| 20 | mbedtls_cipher_context_t encryption_context; | ||
| 21 | mbedtls_cipher_context_t decryption_context; | ||
| 22 | }; | ||
| 23 | |||
| 24 | template <typename Key, size_t KeySize> | ||
| 25 | Crypto::AESCipher<Key, KeySize>::AESCipher(Key key, Mode mode) | ||
| 26 | : ctx(std::make_unique<CipherContext>()) { | ||
| 27 | mbedtls_cipher_init(&ctx->encryption_context); | ||
| 28 | mbedtls_cipher_init(&ctx->decryption_context); | ||
| 29 | |||
| 30 | ASSERT_MSG((mbedtls_cipher_setup( | ||
| 31 | &ctx->encryption_context, | ||
| 32 | mbedtls_cipher_info_from_type(static_cast<mbedtls_cipher_type_t>(mode))) || | ||
| 33 | mbedtls_cipher_setup( | ||
| 34 | &ctx->decryption_context, | ||
| 35 | mbedtls_cipher_info_from_type(static_cast<mbedtls_cipher_type_t>(mode)))) == 0, | ||
| 36 | "Failed to initialize mbedtls ciphers."); | ||
| 37 | |||
| 38 | ASSERT( | ||
| 39 | !mbedtls_cipher_setkey(&ctx->encryption_context, key.data(), KeySize * 8, MBEDTLS_ENCRYPT)); | ||
| 40 | ASSERT( | ||
| 41 | !mbedtls_cipher_setkey(&ctx->decryption_context, key.data(), KeySize * 8, MBEDTLS_DECRYPT)); | ||
| 42 | //"Failed to set key on mbedtls ciphers."); | ||
| 43 | } | ||
| 44 | |||
| 45 | template <typename Key, size_t KeySize> | ||
| 46 | AESCipher<Key, KeySize>::~AESCipher() { | ||
| 47 | mbedtls_cipher_free(&ctx->encryption_context); | ||
| 48 | mbedtls_cipher_free(&ctx->decryption_context); | ||
| 49 | } | ||
| 50 | |||
| 51 | template <typename Key, size_t KeySize> | ||
| 52 | void AESCipher<Key, KeySize>::SetIV(std::vector<u8> iv) { | ||
| 53 | ASSERT_MSG((mbedtls_cipher_set_iv(&ctx->encryption_context, iv.data(), iv.size()) || | ||
| 54 | mbedtls_cipher_set_iv(&ctx->decryption_context, iv.data(), iv.size())) == 0, | ||
| 55 | "Failed to set IV on mbedtls ciphers."); | ||
| 56 | } | ||
| 57 | |||
| 58 | template <typename Key, size_t KeySize> | ||
| 59 | void AESCipher<Key, KeySize>::Transcode(const u8* src, size_t size, u8* dest, Op op) { | ||
| 60 | size_t written = 0; | ||
| 61 | |||
| 62 | const auto context = op == Op::Encrypt ? &ctx->encryption_context : &ctx->decryption_context; | ||
| 63 | |||
| 64 | mbedtls_cipher_reset(context); | ||
| 65 | |||
| 66 | if (mbedtls_cipher_get_cipher_mode(context) == MBEDTLS_MODE_XTS) { | ||
| 67 | mbedtls_cipher_update(context, src, size, dest, &written); | ||
| 68 | if (written != size) | ||
| 69 | LOG_WARNING(Crypto, "Not all data was decrypted requested={:016X}, actual={:016X}.", | ||
| 70 | size, written); | ||
| 71 | } else { | ||
| 72 | const auto block_size = mbedtls_cipher_get_block_size(context); | ||
| 73 | |||
| 74 | for (size_t offset = 0; offset < size; offset += block_size) { | ||
| 75 | auto length = std::min<size_t>(block_size, size - offset); | ||
| 76 | mbedtls_cipher_update(context, src + offset, length, dest + offset, &written); | ||
| 77 | if (written != length) | ||
| 78 | LOG_WARNING(Crypto, "Not all data was decrypted requested={:016X}, actual={:016X}.", | ||
| 79 | length, written); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | mbedtls_cipher_finish(context, nullptr, nullptr); | ||
| 84 | } | ||
| 85 | |||
| 86 | template <typename Key, size_t KeySize> | ||
| 87 | void AESCipher<Key, KeySize>::XTSTranscode(const u8* src, size_t size, u8* dest, size_t sector_id, | ||
| 88 | size_t sector_size, Op op) { | ||
| 89 | if (size % sector_size > 0) { | ||
| 90 | LOG_CRITICAL(Crypto, "Data size must be a multiple of sector size."); | ||
| 91 | return; | ||
| 92 | } | ||
| 93 | |||
| 94 | for (size_t i = 0; i < size; i += sector_size) { | ||
| 95 | SetIV(CalculateNintendoTweak(sector_id++)); | ||
| 96 | Transcode<u8, u8>(src + i, sector_size, dest + i, op); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | template <typename Key, size_t KeySize> | ||
| 101 | std::vector<u8> AESCipher<Key, KeySize>::CalculateNintendoTweak(size_t sector_id) { | ||
| 102 | std::vector<u8> out(0x10); | ||
| 103 | for (size_t i = 0xF; i <= 0xF; --i) { | ||
| 104 | out[i] = sector_id & 0xFF; | ||
| 105 | sector_id >>= 8; | ||
| 106 | } | ||
| 107 | return out; | ||
| 108 | } | ||
| 109 | |||
| 110 | template class AESCipher<Key128>; | ||
| 111 | template class AESCipher<Key256>; | ||
| 112 | } // namespace Core::Crypto \ No newline at end of file | ||
diff --git a/src/core/crypto/aes_util.h b/src/core/crypto/aes_util.h new file mode 100644 index 000000000..5b0b02738 --- /dev/null +++ b/src/core/crypto/aes_util.h | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | // Copyright 2018 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <memory> | ||
| 8 | #include <type_traits> | ||
| 9 | #include <vector> | ||
| 10 | #include "common/assert.h" | ||
| 11 | #include "core/file_sys/vfs.h" | ||
| 12 | |||
| 13 | namespace Core::Crypto { | ||
| 14 | |||
| 15 | struct CipherContext; | ||
| 16 | |||
| 17 | enum class Mode { | ||
| 18 | CTR = 11, | ||
| 19 | ECB = 2, | ||
| 20 | XTS = 70, | ||
| 21 | }; | ||
| 22 | |||
| 23 | enum class Op { | ||
| 24 | Encrypt, | ||
| 25 | Decrypt, | ||
| 26 | }; | ||
| 27 | |||
| 28 | template <typename Key, size_t KeySize = sizeof(Key)> | ||
| 29 | class AESCipher { | ||
| 30 | static_assert(std::is_same_v<Key, std::array<u8, KeySize>>, "Key must be std::array of u8."); | ||
| 31 | static_assert(KeySize == 0x10 || KeySize == 0x20, "KeySize must be 128 or 256."); | ||
| 32 | |||
| 33 | public: | ||
| 34 | AESCipher(Key key, Mode mode); | ||
| 35 | |||
| 36 | ~AESCipher(); | ||
| 37 | |||
| 38 | void SetIV(std::vector<u8> iv); | ||
| 39 | |||
| 40 | template <typename Source, typename Dest> | ||
| 41 | void Transcode(const Source* src, size_t size, Dest* dest, Op op) { | ||
| 42 | Transcode(reinterpret_cast<const u8*>(src), size, reinterpret_cast<u8*>(dest), op); | ||
| 43 | } | ||
| 44 | |||
| 45 | void Transcode(const u8* src, size_t size, u8* dest, Op op); | ||
| 46 | |||
| 47 | template <typename Source, typename Dest> | ||
| 48 | void XTSTranscode(const Source* src, size_t size, Dest* dest, size_t sector_id, | ||
| 49 | size_t sector_size, Op op) { | ||
| 50 | XTSTranscode(reinterpret_cast<const u8*>(src), size, reinterpret_cast<u8*>(dest), sector_id, | ||
| 51 | sector_size, op); | ||
| 52 | } | ||
| 53 | |||
| 54 | void XTSTranscode(const u8* src, size_t size, u8* dest, size_t sector_id, size_t sector_size, | ||
| 55 | Op op); | ||
| 56 | |||
| 57 | private: | ||
| 58 | std::unique_ptr<CipherContext> ctx; | ||
| 59 | |||
| 60 | static std::vector<u8> CalculateNintendoTweak(size_t sector_id); | ||
| 61 | }; | ||
| 62 | } // namespace Core::Crypto | ||
diff --git a/src/core/crypto/ctr_encryption_layer.cpp b/src/core/crypto/ctr_encryption_layer.cpp new file mode 100644 index 000000000..106db02b3 --- /dev/null +++ b/src/core/crypto/ctr_encryption_layer.cpp | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | // Copyright 2018 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <cstring> | ||
| 6 | #include "common/assert.h" | ||
| 7 | #include "core/crypto/ctr_encryption_layer.h" | ||
| 8 | |||
| 9 | namespace Core::Crypto { | ||
| 10 | |||
| 11 | CTREncryptionLayer::CTREncryptionLayer(FileSys::VirtualFile base_, Key128 key_, size_t base_offset) | ||
| 12 | : EncryptionLayer(std::move(base_)), base_offset(base_offset), cipher(key_, Mode::CTR), | ||
| 13 | iv(16, 0) {} | ||
| 14 | |||
| 15 | size_t CTREncryptionLayer::Read(u8* data, size_t length, size_t offset) const { | ||
| 16 | if (length == 0) | ||
| 17 | return 0; | ||
| 18 | |||
| 19 | const auto sector_offset = offset & 0xF; | ||
| 20 | if (sector_offset == 0) { | ||
| 21 | UpdateIV(base_offset + offset); | ||
| 22 | std::vector<u8> raw = base->ReadBytes(length, offset); | ||
| 23 | if (raw.size() != length) | ||
| 24 | return Read(data, raw.size(), offset); | ||
| 25 | cipher.Transcode(raw.data(), length, data, Op::Decrypt); | ||
| 26 | return length; | ||
| 27 | } | ||
| 28 | |||
| 29 | // offset does not fall on block boundary (0x10) | ||
| 30 | std::vector<u8> block = base->ReadBytes(0x10, offset - sector_offset); | ||
| 31 | UpdateIV(base_offset + offset - sector_offset); | ||
| 32 | cipher.Transcode(block.data(), block.size(), block.data(), Op::Decrypt); | ||
| 33 | size_t read = 0x10 - sector_offset; | ||
| 34 | |||
| 35 | if (length + sector_offset < 0x10) { | ||
| 36 | std::memcpy(data, block.data() + sector_offset, std::min<u64>(length, read)); | ||
| 37 | return read; | ||
| 38 | } | ||
| 39 | std::memcpy(data, block.data() + sector_offset, read); | ||
| 40 | return read + Read(data + read, length - read, offset + read); | ||
| 41 | } | ||
| 42 | |||
| 43 | void CTREncryptionLayer::SetIV(const std::vector<u8>& iv_) { | ||
| 44 | const auto length = std::min(iv_.size(), iv.size()); | ||
| 45 | iv.assign(iv_.cbegin(), iv_.cbegin() + length); | ||
| 46 | } | ||
| 47 | |||
| 48 | void CTREncryptionLayer::UpdateIV(size_t offset) const { | ||
| 49 | offset >>= 4; | ||
| 50 | for (size_t i = 0; i < 8; ++i) { | ||
| 51 | iv[16 - i - 1] = offset & 0xFF; | ||
| 52 | offset >>= 8; | ||
| 53 | } | ||
| 54 | cipher.SetIV(iv); | ||
| 55 | } | ||
| 56 | } // namespace Core::Crypto | ||
diff --git a/src/core/crypto/ctr_encryption_layer.h b/src/core/crypto/ctr_encryption_layer.h new file mode 100644 index 000000000..11b8683c7 --- /dev/null +++ b/src/core/crypto/ctr_encryption_layer.h | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | // Copyright 2018 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <vector> | ||
| 8 | #include "core/crypto/aes_util.h" | ||
| 9 | #include "core/crypto/encryption_layer.h" | ||
| 10 | #include "core/crypto/key_manager.h" | ||
| 11 | |||
| 12 | namespace Core::Crypto { | ||
| 13 | |||
| 14 | // Sits on top of a VirtualFile and provides CTR-mode AES decription. | ||
| 15 | class CTREncryptionLayer : public EncryptionLayer { | ||
| 16 | public: | ||
| 17 | CTREncryptionLayer(FileSys::VirtualFile base, Key128 key, size_t base_offset); | ||
| 18 | |||
| 19 | size_t Read(u8* data, size_t length, size_t offset) const override; | ||
| 20 | |||
| 21 | void SetIV(const std::vector<u8>& iv); | ||
| 22 | |||
| 23 | private: | ||
| 24 | size_t base_offset; | ||
| 25 | |||
| 26 | // Must be mutable as operations modify cipher contexts. | ||
| 27 | mutable AESCipher<Key128> cipher; | ||
| 28 | mutable std::vector<u8> iv; | ||
| 29 | |||
| 30 | void UpdateIV(size_t offset) const; | ||
| 31 | }; | ||
| 32 | |||
| 33 | } // namespace Core::Crypto | ||
diff --git a/src/core/crypto/encryption_layer.cpp b/src/core/crypto/encryption_layer.cpp new file mode 100644 index 000000000..4204527e3 --- /dev/null +++ b/src/core/crypto/encryption_layer.cpp | |||
| @@ -0,0 +1,42 @@ | |||
| 1 | // Copyright 2018 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include "core/crypto/encryption_layer.h" | ||
| 6 | |||
| 7 | namespace Core::Crypto { | ||
| 8 | |||
| 9 | EncryptionLayer::EncryptionLayer(FileSys::VirtualFile base_) : base(std::move(base_)) {} | ||
| 10 | |||
| 11 | std::string EncryptionLayer::GetName() const { | ||
| 12 | return base->GetName(); | ||
| 13 | } | ||
| 14 | |||
| 15 | size_t EncryptionLayer::GetSize() const { | ||
| 16 | return base->GetSize(); | ||
| 17 | } | ||
| 18 | |||
| 19 | bool EncryptionLayer::Resize(size_t new_size) { | ||
| 20 | return false; | ||
| 21 | } | ||
| 22 | |||
| 23 | std::shared_ptr<FileSys::VfsDirectory> EncryptionLayer::GetContainingDirectory() const { | ||
| 24 | return base->GetContainingDirectory(); | ||
| 25 | } | ||
| 26 | |||
| 27 | bool EncryptionLayer::IsWritable() const { | ||
| 28 | return false; | ||
| 29 | } | ||
| 30 | |||
| 31 | bool EncryptionLayer::IsReadable() const { | ||
| 32 | return true; | ||
| 33 | } | ||
| 34 | |||
| 35 | size_t EncryptionLayer::Write(const u8* data, size_t length, size_t offset) { | ||
| 36 | return 0; | ||
| 37 | } | ||
| 38 | |||
| 39 | bool EncryptionLayer::Rename(std::string_view name) { | ||
| 40 | return base->Rename(name); | ||
| 41 | } | ||
| 42 | } // namespace Core::Crypto | ||
diff --git a/src/core/crypto/encryption_layer.h b/src/core/crypto/encryption_layer.h new file mode 100644 index 000000000..71bca1f23 --- /dev/null +++ b/src/core/crypto/encryption_layer.h | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | // Copyright 2018 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "core/file_sys/vfs.h" | ||
| 8 | |||
| 9 | namespace Core::Crypto { | ||
| 10 | |||
| 11 | // Basically non-functional class that implements all of the methods that are irrelevant to an | ||
| 12 | // EncryptionLayer. Reduces duplicate code. | ||
| 13 | class EncryptionLayer : public FileSys::VfsFile { | ||
| 14 | public: | ||
| 15 | explicit EncryptionLayer(FileSys::VirtualFile base); | ||
| 16 | |||
| 17 | size_t Read(u8* data, size_t length, size_t offset) const override = 0; | ||
| 18 | |||
| 19 | std::string GetName() const override; | ||
| 20 | size_t GetSize() const override; | ||
| 21 | bool Resize(size_t new_size) override; | ||
| 22 | std::shared_ptr<FileSys::VfsDirectory> GetContainingDirectory() const override; | ||
| 23 | bool IsWritable() const override; | ||
| 24 | bool IsReadable() const override; | ||
| 25 | size_t Write(const u8* data, size_t length, size_t offset) override; | ||
| 26 | bool Rename(std::string_view name) override; | ||
| 27 | |||
| 28 | protected: | ||
| 29 | FileSys::VirtualFile base; | ||
| 30 | }; | ||
| 31 | |||
| 32 | } // namespace Core::Crypto | ||
diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp new file mode 100644 index 000000000..678ac5752 --- /dev/null +++ b/src/core/crypto/key_manager.cpp | |||
| @@ -0,0 +1,215 @@ | |||
| 1 | // Copyright 2018 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <array> | ||
| 6 | #include <fstream> | ||
| 7 | #include <locale> | ||
| 8 | #include <sstream> | ||
| 9 | #include <string_view> | ||
| 10 | #include <mbedtls/sha256.h> | ||
| 11 | #include "common/assert.h" | ||
| 12 | #include "common/common_paths.h" | ||
| 13 | #include "common/file_util.h" | ||
| 14 | #include "common/logging/log.h" | ||
| 15 | #include "core/crypto/key_manager.h" | ||
| 16 | #include "core/settings.h" | ||
| 17 | #include "key_manager.h" | ||
| 18 | |||
| 19 | namespace Core::Crypto { | ||
| 20 | |||
| 21 | static u8 ToHexNibble(char c1) { | ||
| 22 | if (c1 >= 65 && c1 <= 70) | ||
| 23 | return c1 - 55; | ||
| 24 | if (c1 >= 97 && c1 <= 102) | ||
| 25 | return c1 - 87; | ||
| 26 | if (c1 >= 48 && c1 <= 57) | ||
| 27 | return c1 - 48; | ||
| 28 | throw std::logic_error("Invalid hex digit"); | ||
| 29 | } | ||
| 30 | |||
| 31 | template <size_t Size> | ||
| 32 | static std::array<u8, Size> HexStringToArray(std::string_view str) { | ||
| 33 | std::array<u8, Size> out{}; | ||
| 34 | for (size_t i = 0; i < 2 * Size; i += 2) { | ||
| 35 | auto d1 = str[i]; | ||
| 36 | auto d2 = str[i + 1]; | ||
| 37 | out[i / 2] = (ToHexNibble(d1) << 4) | ToHexNibble(d2); | ||
| 38 | } | ||
| 39 | return out; | ||
| 40 | } | ||
| 41 | |||
| 42 | std::array<u8, 16> operator""_array16(const char* str, size_t len) { | ||
| 43 | if (len != 32) | ||
| 44 | throw std::logic_error("Not of correct size."); | ||
| 45 | return HexStringToArray<16>(str); | ||
| 46 | } | ||
| 47 | |||
| 48 | std::array<u8, 32> operator""_array32(const char* str, size_t len) { | ||
| 49 | if (len != 64) | ||
| 50 | throw std::logic_error("Not of correct size."); | ||
| 51 | return HexStringToArray<32>(str); | ||
| 52 | } | ||
| 53 | |||
| 54 | KeyManager::KeyManager() { | ||
| 55 | // Initialize keys | ||
| 56 | const std::string hactool_keys_dir = FileUtil::GetHactoolConfigurationPath(); | ||
| 57 | const std::string yuzu_keys_dir = FileUtil::GetUserPath(FileUtil::UserPath::KeysDir); | ||
| 58 | if (Settings::values.use_dev_keys) { | ||
| 59 | dev_mode = true; | ||
| 60 | AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "dev.keys", false); | ||
| 61 | } else { | ||
| 62 | dev_mode = false; | ||
| 63 | AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "prod.keys", false); | ||
| 64 | } | ||
| 65 | |||
| 66 | AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "title.keys", true); | ||
| 67 | } | ||
| 68 | |||
| 69 | void KeyManager::LoadFromFile(std::string_view filename_, bool is_title_keys) { | ||
| 70 | const auto filename = std::string(filename_); | ||
| 71 | std::ifstream file(filename); | ||
| 72 | if (!file.is_open()) | ||
| 73 | return; | ||
| 74 | |||
| 75 | std::string line; | ||
| 76 | while (std::getline(file, line)) { | ||
| 77 | std::vector<std::string> out; | ||
| 78 | std::stringstream stream(line); | ||
| 79 | std::string item; | ||
| 80 | while (std::getline(stream, item, '=')) | ||
| 81 | out.push_back(std::move(item)); | ||
| 82 | |||
| 83 | if (out.size() != 2) | ||
| 84 | continue; | ||
| 85 | |||
| 86 | out[0].erase(std::remove(out[0].begin(), out[0].end(), ' '), out[0].end()); | ||
| 87 | out[1].erase(std::remove(out[1].begin(), out[1].end(), ' '), out[1].end()); | ||
| 88 | |||
| 89 | if (is_title_keys) { | ||
| 90 | auto rights_id_raw = HexStringToArray<16>(out[0]); | ||
| 91 | u128 rights_id{}; | ||
| 92 | std::memcpy(rights_id.data(), rights_id_raw.data(), rights_id_raw.size()); | ||
| 93 | Key128 key = HexStringToArray<16>(out[1]); | ||
| 94 | SetKey(S128KeyType::Titlekey, key, rights_id[1], rights_id[0]); | ||
| 95 | } else { | ||
| 96 | std::transform(out[0].begin(), out[0].end(), out[0].begin(), ::tolower); | ||
| 97 | if (s128_file_id.find(out[0]) != s128_file_id.end()) { | ||
| 98 | const auto index = s128_file_id.at(out[0]); | ||
| 99 | Key128 key = HexStringToArray<16>(out[1]); | ||
| 100 | SetKey(index.type, key, index.field1, index.field2); | ||
| 101 | } else if (s256_file_id.find(out[0]) != s256_file_id.end()) { | ||
| 102 | const auto index = s256_file_id.at(out[0]); | ||
| 103 | Key256 key = HexStringToArray<32>(out[1]); | ||
| 104 | SetKey(index.type, key, index.field1, index.field2); | ||
| 105 | } | ||
| 106 | } | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | void KeyManager::AttemptLoadKeyFile(std::string_view dir1_, std::string_view dir2_, | ||
| 111 | std::string_view filename_, bool title) { | ||
| 112 | const std::string dir1(dir1_); | ||
| 113 | const std::string dir2(dir2_); | ||
| 114 | const std::string filename(filename_); | ||
| 115 | if (FileUtil::Exists(dir1 + DIR_SEP + filename)) | ||
| 116 | LoadFromFile(dir1 + DIR_SEP + filename, title); | ||
| 117 | else if (FileUtil::Exists(dir2 + DIR_SEP + filename)) | ||
| 118 | LoadFromFile(dir2 + DIR_SEP + filename, title); | ||
| 119 | } | ||
| 120 | |||
| 121 | bool KeyManager::HasKey(S128KeyType id, u64 field1, u64 field2) const { | ||
| 122 | return s128_keys.find({id, field1, field2}) != s128_keys.end(); | ||
| 123 | } | ||
| 124 | |||
| 125 | bool KeyManager::HasKey(S256KeyType id, u64 field1, u64 field2) const { | ||
| 126 | return s256_keys.find({id, field1, field2}) != s256_keys.end(); | ||
| 127 | } | ||
| 128 | |||
| 129 | Key128 KeyManager::GetKey(S128KeyType id, u64 field1, u64 field2) const { | ||
| 130 | if (!HasKey(id, field1, field2)) | ||
| 131 | return {}; | ||
| 132 | return s128_keys.at({id, field1, field2}); | ||
| 133 | } | ||
| 134 | |||
| 135 | Key256 KeyManager::GetKey(S256KeyType id, u64 field1, u64 field2) const { | ||
| 136 | if (!HasKey(id, field1, field2)) | ||
| 137 | return {}; | ||
| 138 | return s256_keys.at({id, field1, field2}); | ||
| 139 | } | ||
| 140 | |||
| 141 | void KeyManager::SetKey(S128KeyType id, Key128 key, u64 field1, u64 field2) { | ||
| 142 | s128_keys[{id, field1, field2}] = key; | ||
| 143 | } | ||
| 144 | |||
| 145 | void KeyManager::SetKey(S256KeyType id, Key256 key, u64 field1, u64 field2) { | ||
| 146 | s256_keys[{id, field1, field2}] = key; | ||
| 147 | } | ||
| 148 | |||
| 149 | bool KeyManager::KeyFileExists(bool title) { | ||
| 150 | const std::string hactool_keys_dir = FileUtil::GetHactoolConfigurationPath(); | ||
| 151 | const std::string yuzu_keys_dir = FileUtil::GetUserPath(FileUtil::UserPath::KeysDir); | ||
| 152 | if (title) { | ||
| 153 | return FileUtil::Exists(hactool_keys_dir + DIR_SEP + "title.keys") || | ||
| 154 | FileUtil::Exists(yuzu_keys_dir + DIR_SEP + "title.keys"); | ||
| 155 | } | ||
| 156 | |||
| 157 | if (Settings::values.use_dev_keys) { | ||
| 158 | return FileUtil::Exists(hactool_keys_dir + DIR_SEP + "dev.keys") || | ||
| 159 | FileUtil::Exists(yuzu_keys_dir + DIR_SEP + "dev.keys"); | ||
| 160 | } | ||
| 161 | |||
| 162 | return FileUtil::Exists(hactool_keys_dir + DIR_SEP + "prod.keys") || | ||
| 163 | FileUtil::Exists(yuzu_keys_dir + DIR_SEP + "prod.keys"); | ||
| 164 | } | ||
| 165 | |||
| 166 | const std::unordered_map<std::string, KeyIndex<S128KeyType>> KeyManager::s128_file_id = { | ||
| 167 | {"master_key_00", {S128KeyType::Master, 0, 0}}, | ||
| 168 | {"master_key_01", {S128KeyType::Master, 1, 0}}, | ||
| 169 | {"master_key_02", {S128KeyType::Master, 2, 0}}, | ||
| 170 | {"master_key_03", {S128KeyType::Master, 3, 0}}, | ||
| 171 | {"master_key_04", {S128KeyType::Master, 4, 0}}, | ||
| 172 | {"package1_key_00", {S128KeyType::Package1, 0, 0}}, | ||
| 173 | {"package1_key_01", {S128KeyType::Package1, 1, 0}}, | ||
| 174 | {"package1_key_02", {S128KeyType::Package1, 2, 0}}, | ||
| 175 | {"package1_key_03", {S128KeyType::Package1, 3, 0}}, | ||
| 176 | {"package1_key_04", {S128KeyType::Package1, 4, 0}}, | ||
| 177 | {"package2_key_00", {S128KeyType::Package2, 0, 0}}, | ||
| 178 | {"package2_key_01", {S128KeyType::Package2, 1, 0}}, | ||
| 179 | {"package2_key_02", {S128KeyType::Package2, 2, 0}}, | ||
| 180 | {"package2_key_03", {S128KeyType::Package2, 3, 0}}, | ||
| 181 | {"package2_key_04", {S128KeyType::Package2, 4, 0}}, | ||
| 182 | {"titlekek_00", {S128KeyType::Titlekek, 0, 0}}, | ||
| 183 | {"titlekek_01", {S128KeyType::Titlekek, 1, 0}}, | ||
| 184 | {"titlekek_02", {S128KeyType::Titlekek, 2, 0}}, | ||
| 185 | {"titlekek_03", {S128KeyType::Titlekek, 3, 0}}, | ||
| 186 | {"titlekek_04", {S128KeyType::Titlekek, 4, 0}}, | ||
| 187 | {"eticket_rsa_kek", {S128KeyType::ETicketRSAKek, 0, 0}}, | ||
| 188 | {"key_area_key_application_00", | ||
| 189 | {S128KeyType::KeyArea, 0, static_cast<u64>(KeyAreaKeyType::Application)}}, | ||
| 190 | {"key_area_key_application_01", | ||
| 191 | {S128KeyType::KeyArea, 1, static_cast<u64>(KeyAreaKeyType::Application)}}, | ||
| 192 | {"key_area_key_application_02", | ||
| 193 | {S128KeyType::KeyArea, 2, static_cast<u64>(KeyAreaKeyType::Application)}}, | ||
| 194 | {"key_area_key_application_03", | ||
| 195 | {S128KeyType::KeyArea, 3, static_cast<u64>(KeyAreaKeyType::Application)}}, | ||
| 196 | {"key_area_key_application_04", | ||
| 197 | {S128KeyType::KeyArea, 4, static_cast<u64>(KeyAreaKeyType::Application)}}, | ||
| 198 | {"key_area_key_ocean_00", {S128KeyType::KeyArea, 0, static_cast<u64>(KeyAreaKeyType::Ocean)}}, | ||
| 199 | {"key_area_key_ocean_01", {S128KeyType::KeyArea, 1, static_cast<u64>(KeyAreaKeyType::Ocean)}}, | ||
| 200 | {"key_area_key_ocean_02", {S128KeyType::KeyArea, 2, static_cast<u64>(KeyAreaKeyType::Ocean)}}, | ||
| 201 | {"key_area_key_ocean_03", {S128KeyType::KeyArea, 3, static_cast<u64>(KeyAreaKeyType::Ocean)}}, | ||
| 202 | {"key_area_key_ocean_04", {S128KeyType::KeyArea, 4, static_cast<u64>(KeyAreaKeyType::Ocean)}}, | ||
| 203 | {"key_area_key_system_00", {S128KeyType::KeyArea, 0, static_cast<u64>(KeyAreaKeyType::System)}}, | ||
| 204 | {"key_area_key_system_01", {S128KeyType::KeyArea, 1, static_cast<u64>(KeyAreaKeyType::System)}}, | ||
| 205 | {"key_area_key_system_02", {S128KeyType::KeyArea, 2, static_cast<u64>(KeyAreaKeyType::System)}}, | ||
| 206 | {"key_area_key_system_03", {S128KeyType::KeyArea, 3, static_cast<u64>(KeyAreaKeyType::System)}}, | ||
| 207 | {"key_area_key_system_04", {S128KeyType::KeyArea, 4, static_cast<u64>(KeyAreaKeyType::System)}}, | ||
| 208 | }; | ||
| 209 | |||
| 210 | const std::unordered_map<std::string, KeyIndex<S256KeyType>> KeyManager::s256_file_id = { | ||
| 211 | {"header_key", {S256KeyType::Header, 0, 0}}, | ||
| 212 | {"sd_card_save_key", {S256KeyType::SDSave, 0, 0}}, | ||
| 213 | {"sd_card_nca_key", {S256KeyType::SDNCA, 0, 0}}, | ||
| 214 | }; | ||
| 215 | } // namespace Core::Crypto | ||
diff --git a/src/core/crypto/key_manager.h b/src/core/crypto/key_manager.h new file mode 100644 index 000000000..03152a12c --- /dev/null +++ b/src/core/crypto/key_manager.h | |||
| @@ -0,0 +1,119 @@ | |||
| 1 | // Copyright 2018 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include <type_traits> | ||
| 9 | #include <unordered_map> | ||
| 10 | #include <vector> | ||
| 11 | #include <fmt/format.h> | ||
| 12 | #include "common/common_types.h" | ||
| 13 | |||
| 14 | namespace Core::Crypto { | ||
| 15 | |||
| 16 | using Key128 = std::array<u8, 0x10>; | ||
| 17 | using Key256 = std::array<u8, 0x20>; | ||
| 18 | using SHA256Hash = std::array<u8, 0x20>; | ||
| 19 | |||
| 20 | static_assert(sizeof(Key128) == 16, "Key128 must be 128 bytes big."); | ||
| 21 | static_assert(sizeof(Key256) == 32, "Key128 must be 128 bytes big."); | ||
| 22 | |||
| 23 | enum class S256KeyType : u64 { | ||
| 24 | Header, // | ||
| 25 | SDSave, // | ||
| 26 | SDNCA, // | ||
| 27 | }; | ||
| 28 | |||
| 29 | enum class S128KeyType : u64 { | ||
| 30 | Master, // f1=crypto revision | ||
| 31 | Package1, // f1=crypto revision | ||
| 32 | Package2, // f1=crypto revision | ||
| 33 | Titlekek, // f1=crypto revision | ||
| 34 | ETicketRSAKek, // | ||
| 35 | KeyArea, // f1=crypto revision f2=type {app, ocean, system} | ||
| 36 | SDSeed, // | ||
| 37 | Titlekey, // f1=rights id LSB f2=rights id MSB | ||
| 38 | }; | ||
| 39 | |||
| 40 | enum class KeyAreaKeyType : u8 { | ||
| 41 | Application, | ||
| 42 | Ocean, | ||
| 43 | System, | ||
| 44 | }; | ||
| 45 | |||
| 46 | template <typename KeyType> | ||
| 47 | struct KeyIndex { | ||
| 48 | KeyType type; | ||
| 49 | u64 field1; | ||
| 50 | u64 field2; | ||
| 51 | |||
| 52 | std::string DebugInfo() const { | ||
| 53 | u8 key_size = 16; | ||
| 54 | if constexpr (std::is_same_v<KeyType, S256KeyType>) | ||
| 55 | key_size = 32; | ||
| 56 | return fmt::format("key_size={:02X}, key={:02X}, field1={:016X}, field2={:016X}", key_size, | ||
| 57 | static_cast<u8>(type), field1, field2); | ||
| 58 | } | ||
| 59 | }; | ||
| 60 | |||
| 61 | // The following two (== and hash) are so KeyIndex can be a key in unordered_map | ||
| 62 | |||
| 63 | template <typename KeyType> | ||
| 64 | bool operator==(const KeyIndex<KeyType>& lhs, const KeyIndex<KeyType>& rhs) { | ||
| 65 | return std::tie(lhs.type, lhs.field1, lhs.field2) == std::tie(rhs.type, rhs.field1, rhs.field2); | ||
| 66 | } | ||
| 67 | |||
| 68 | template <typename KeyType> | ||
| 69 | bool operator!=(const KeyIndex<KeyType>& lhs, const KeyIndex<KeyType>& rhs) { | ||
| 70 | return !operator==(lhs, rhs); | ||
| 71 | } | ||
| 72 | |||
| 73 | } // namespace Core::Crypto | ||
| 74 | |||
| 75 | namespace std { | ||
| 76 | template <typename KeyType> | ||
| 77 | struct hash<Core::Crypto::KeyIndex<KeyType>> { | ||
| 78 | size_t operator()(const Core::Crypto::KeyIndex<KeyType>& k) const { | ||
| 79 | using std::hash; | ||
| 80 | |||
| 81 | return ((hash<u64>()(static_cast<u64>(k.type)) ^ (hash<u64>()(k.field1) << 1)) >> 1) ^ | ||
| 82 | (hash<u64>()(k.field2) << 1); | ||
| 83 | } | ||
| 84 | }; | ||
| 85 | } // namespace std | ||
| 86 | |||
| 87 | namespace Core::Crypto { | ||
| 88 | |||
| 89 | std::array<u8, 0x10> operator"" _array16(const char* str, size_t len); | ||
| 90 | std::array<u8, 0x20> operator"" _array32(const char* str, size_t len); | ||
| 91 | |||
| 92 | class KeyManager { | ||
| 93 | public: | ||
| 94 | KeyManager(); | ||
| 95 | |||
| 96 | bool HasKey(S128KeyType id, u64 field1 = 0, u64 field2 = 0) const; | ||
| 97 | bool HasKey(S256KeyType id, u64 field1 = 0, u64 field2 = 0) const; | ||
| 98 | |||
| 99 | Key128 GetKey(S128KeyType id, u64 field1 = 0, u64 field2 = 0) const; | ||
| 100 | Key256 GetKey(S256KeyType id, u64 field1 = 0, u64 field2 = 0) const; | ||
| 101 | |||
| 102 | void SetKey(S128KeyType id, Key128 key, u64 field1 = 0, u64 field2 = 0); | ||
| 103 | void SetKey(S256KeyType id, Key256 key, u64 field1 = 0, u64 field2 = 0); | ||
| 104 | |||
| 105 | static bool KeyFileExists(bool title); | ||
| 106 | |||
| 107 | private: | ||
| 108 | std::unordered_map<KeyIndex<S128KeyType>, Key128> s128_keys; | ||
| 109 | std::unordered_map<KeyIndex<S256KeyType>, Key256> s256_keys; | ||
| 110 | |||
| 111 | bool dev_mode; | ||
| 112 | void LoadFromFile(std::string_view filename, bool is_title_keys); | ||
| 113 | void AttemptLoadKeyFile(std::string_view dir1, std::string_view dir2, std::string_view filename, | ||
| 114 | bool title); | ||
| 115 | |||
| 116 | static const std::unordered_map<std::string, KeyIndex<S128KeyType>> s128_file_id; | ||
| 117 | static const std::unordered_map<std::string, KeyIndex<S256KeyType>> s256_file_id; | ||
| 118 | }; | ||
| 119 | } // namespace Core::Crypto | ||
diff --git a/src/core/crypto/sha_util.cpp b/src/core/crypto/sha_util.cpp new file mode 100644 index 000000000..180008a85 --- /dev/null +++ b/src/core/crypto/sha_util.cpp | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | // Copyright 2018 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | namespace Crypto {} // namespace Crypto | ||
diff --git a/src/core/crypto/sha_util.h b/src/core/crypto/sha_util.h new file mode 100644 index 000000000..fa3fa9d33 --- /dev/null +++ b/src/core/crypto/sha_util.h | |||
| @@ -0,0 +1,20 @@ | |||
| 1 | // Copyright 2018 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "common/assert.h" | ||
| 8 | #include "core/file_sys/vfs.h" | ||
| 9 | #include "key_manager.h" | ||
| 10 | #include "mbedtls/cipher.h" | ||
| 11 | |||
| 12 | namespace Crypto { | ||
| 13 | typedef std::array<u8, 0x20> SHA256Hash; | ||
| 14 | |||
| 15 | inline SHA256Hash operator"" _HASH(const char* data, size_t len) { | ||
| 16 | if (len != 0x40) | ||
| 17 | return {}; | ||
| 18 | } | ||
| 19 | |||
| 20 | } // namespace Crypto | ||