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/key_manager.h | 116 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 src/core/crypto/key_manager.h (limited to 'src/core/crypto/key_manager.h') diff --git a/src/core/crypto/key_manager.h b/src/core/crypto/key_manager.h new file mode 100644 index 000000000..155989e46 --- /dev/null +++ b/src/core/crypto/key_manager.h @@ -0,0 +1,116 @@ +// Copyright 2018 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once +#include +#include +#include +#include "common/common_types.h" + +namespace Crypto { + +typedef std::array Key128; +typedef std::array Key256; +typedef std::array SHA256Hash; + +static_assert(sizeof(Key128) == 16, "Key128 must be 128 bytes big."); +static_assert(sizeof(Key256) == 32, "Key128 must be 128 bytes big."); + +enum class S256KeyType : u64 { + HEADER, // + SD_SAVE, // + SD_NCA, // +}; + +enum class S128KeyType : u64 { + MASTER, // f1=crypto revision + PACKAGE1, // f1=crypto revision + PACKAGE2, // f1=crypto revision + TITLEKEK, // f1=crypto revision + ETICKET_RSA_KEK, // + KEY_AREA, // f1=crypto revision f2=type {app, ocean, system} + SD_SEED, // + TITLEKEY, // f1=rights id LSB f2=rights id MSB +}; + +enum class KeyAreaKeyType : u8 { + Application, + Ocean, + System, +}; + +template +struct KeyIndex { + KeyType type; + u64 field1; + u64 field2; + + std::string DebugInfo() { + u8 key_size = 16; + if (std::is_same_v) + key_size = 32; + return fmt::format("key_size={:02X}, key={:02X}, field1={:016X}, field2={:016X}", key_size, + static_cast(type), field1, field2); + } +}; + +// The following two (== and hash) are so KeyIndex can be a key in unordered_map + +template +bool operator==(const KeyIndex& lhs, const KeyIndex& rhs) { + return lhs.type == rhs.type && lhs.field1 == rhs.field1 && lhs.field2 == rhs.field2; +} + +} // namespace Crypto + +namespace std { +template +struct hash> { + size_t operator()(const Crypto::KeyIndex& k) const { + using std::hash; + + return ((hash()(static_cast(k.type)) ^ (hash()(k.field1) << 1)) >> 1) ^ + (hash()(k.field2) << 1); + } +}; +} // namespace std + +namespace Crypto { + +std::array operator"" _array16(const char* str, size_t len); +std::array operator"" _array32(const char* str, size_t len); + +struct KeyManager { + void SetValidationMode(bool dev); + void LoadFromFile(std::string_view filename, bool is_title_keys); + + bool HasKey(S128KeyType id, u64 field1 = 0, u64 field2 = 0); + bool HasKey(S256KeyType id, u64 field1 = 0, u64 field2 = 0); + + Key128 GetKey(S128KeyType id, u64 field1 = 0, u64 field2 = 0); + Key256 GetKey(S256KeyType id, u64 field1 = 0, u64 field2 = 0); + + void SetKey(S128KeyType id, Key128 key, u64 field1 = 0, u64 field2 = 0); + void SetKey(S256KeyType id, Key256 key, u64 field1 = 0, u64 field2 = 0); + + bool ValidateKey(S128KeyType key, u64 field1 = 0, u64 field2 = 0); + bool ValidateKey(S256KeyType key, u64 field1 = 0, u64 field2 = 0); + +private: + std::unordered_map, Key128> s128_keys; + std::unordered_map, Key256> s256_keys; + + bool dev_mode = false; + + static std::unordered_map, SHA256Hash> s128_hash_prod; + static std::unordered_map, SHA256Hash> s256_hash_prod; + static std::unordered_map, SHA256Hash> s128_hash_dev; + static std::unordered_map, SHA256Hash> s256_hash_dev; + static std::unordered_map> s128_file_id; + static std::unordered_map> s256_file_id; +}; + +extern KeyManager keys; + +} // namespace Crypto -- cgit v1.2.3 From c54a10cb4f9912aa0827b8a1b007757252fc90ae Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sat, 28 Jul 2018 14:28:14 -0400 Subject: Update mbedtls and fix compile error --- src/core/crypto/key_manager.h | 1 + 1 file changed, 1 insertion(+) (limited to 'src/core/crypto/key_manager.h') diff --git a/src/core/crypto/key_manager.h b/src/core/crypto/key_manager.h index 155989e46..b892a83f2 100644 --- a/src/core/crypto/key_manager.h +++ b/src/core/crypto/key_manager.h @@ -6,6 +6,7 @@ #include #include #include +#include #include "common/common_types.h" 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/key_manager.h | 77 +++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 40 deletions(-) (limited to 'src/core/crypto/key_manager.h') diff --git a/src/core/crypto/key_manager.h b/src/core/crypto/key_manager.h index b892a83f2..e04f1d49f 100644 --- a/src/core/crypto/key_manager.h +++ b/src/core/crypto/key_manager.h @@ -3,36 +3,37 @@ // Refer to the license.txt file included. #pragma once + #include #include #include #include #include "common/common_types.h" -namespace Crypto { +namespace Core::Crypto { -typedef std::array Key128; -typedef std::array Key256; -typedef std::array SHA256Hash; +using Key128 = std::array; +using Key256 = std::array; +using SHA256Hash = std::array; static_assert(sizeof(Key128) == 16, "Key128 must be 128 bytes big."); static_assert(sizeof(Key256) == 32, "Key128 must be 128 bytes big."); enum class S256KeyType : u64 { - HEADER, // - SD_SAVE, // - SD_NCA, // + Header, // + SDSave, // + SDNCA, // }; enum class S128KeyType : u64 { - MASTER, // f1=crypto revision - PACKAGE1, // f1=crypto revision - PACKAGE2, // f1=crypto revision - TITLEKEK, // f1=crypto revision - ETICKET_RSA_KEK, // - KEY_AREA, // f1=crypto revision f2=type {app, ocean, system} - SD_SEED, // - TITLEKEY, // f1=rights id LSB f2=rights id MSB + Master, // f1=crypto revision + Package1, // f1=crypto revision + Package2, // f1=crypto revision + Titlekek, // f1=crypto revision + ETicketRSAKek, // + KeyArea, // f1=crypto revision f2=type {app, ocean, system} + SDSeed, // + Titlekey, // f1=rights id LSB f2=rights id MSB }; enum class KeyAreaKeyType : u8 { @@ -47,7 +48,7 @@ struct KeyIndex { u64 field1; u64 field2; - std::string DebugInfo() { + std::string DebugInfo() const { u8 key_size = 16; if (std::is_same_v) key_size = 32; @@ -60,15 +61,20 @@ struct KeyIndex { template bool operator==(const KeyIndex& lhs, const KeyIndex& rhs) { - return lhs.type == rhs.type && lhs.field1 == rhs.field1 && lhs.field2 == rhs.field2; + return std::tie(lhs.type, lhs.field1, lhs.field2) == std::tie(rhs.type, rhs.field1, rhs.field2); } -} // namespace Crypto +template +bool operator!=(const KeyIndex& lhs, const KeyIndex& rhs) { + return !operator==(lhs, rhs); +} + +} // namespace Core::Crypto namespace std { template -struct hash> { - size_t operator()(const Crypto::KeyIndex& k) const { +struct hash> { + size_t operator()(const Core::Crypto::KeyIndex& k) const { using std::hash; return ((hash()(static_cast(k.type)) ^ (hash()(k.field1) << 1)) >> 1) ^ @@ -77,41 +83,32 @@ struct hash> { }; } // namespace std -namespace Crypto { +namespace Core::Crypto { std::array operator"" _array16(const char* str, size_t len); std::array operator"" _array32(const char* str, size_t len); -struct KeyManager { - void SetValidationMode(bool dev); - void LoadFromFile(std::string_view filename, bool is_title_keys); +class KeyManager { +public: + KeyManager(); - bool HasKey(S128KeyType id, u64 field1 = 0, u64 field2 = 0); - bool HasKey(S256KeyType id, u64 field1 = 0, u64 field2 = 0); + bool HasKey(S128KeyType id, u64 field1 = 0, u64 field2 = 0) const; + bool HasKey(S256KeyType id, u64 field1 = 0, u64 field2 = 0) const; - Key128 GetKey(S128KeyType id, u64 field1 = 0, u64 field2 = 0); - Key256 GetKey(S256KeyType id, u64 field1 = 0, u64 field2 = 0); + Key128 GetKey(S128KeyType id, u64 field1 = 0, u64 field2 = 0) const; + Key256 GetKey(S256KeyType id, u64 field1 = 0, u64 field2 = 0) const; void SetKey(S128KeyType id, Key128 key, u64 field1 = 0, u64 field2 = 0); void SetKey(S256KeyType id, Key256 key, u64 field1 = 0, u64 field2 = 0); - bool ValidateKey(S128KeyType key, u64 field1 = 0, u64 field2 = 0); - bool ValidateKey(S256KeyType key, u64 field1 = 0, u64 field2 = 0); - private: std::unordered_map, Key128> s128_keys; std::unordered_map, Key256> s256_keys; - bool dev_mode = false; + bool dev_mode; + void LoadFromFile(std::string_view filename, bool is_title_keys); - static std::unordered_map, SHA256Hash> s128_hash_prod; - static std::unordered_map, SHA256Hash> s256_hash_prod; - static std::unordered_map, SHA256Hash> s128_hash_dev; - static std::unordered_map, SHA256Hash> s256_hash_dev; static std::unordered_map> s128_file_id; static std::unordered_map> s256_file_id; }; - -extern KeyManager keys; - -} // namespace Crypto +} // namespace Core::Crypto -- cgit v1.2.3 From 150527ec194107f0ba5ea9bdef487782e64090ef Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sun, 29 Jul 2018 18:42:04 -0400 Subject: Allow key loading from %YUZU_DIR%/keys in addition to ~/.switch --- src/core/crypto/key_manager.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/core/crypto/key_manager.h') diff --git a/src/core/crypto/key_manager.h b/src/core/crypto/key_manager.h index e04f1d49f..a52ea4cb9 100644 --- a/src/core/crypto/key_manager.h +++ b/src/core/crypto/key_manager.h @@ -107,6 +107,8 @@ private: bool dev_mode; void LoadFromFile(std::string_view filename, bool is_title_keys); + void AttemptLoadKeyFile(std::string_view dir1, std::string_view dir2, std::string_view filename, + bool title); static std::unordered_map> s128_file_id; static std::unordered_map> s256_file_id; -- 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/key_manager.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/core/crypto/key_manager.h') diff --git a/src/core/crypto/key_manager.h b/src/core/crypto/key_manager.h index a52ea4cb9..28a560a3f 100644 --- a/src/core/crypto/key_manager.h +++ b/src/core/crypto/key_manager.h @@ -5,6 +5,7 @@ #pragma once #include +#include #include #include #include @@ -50,7 +51,7 @@ struct KeyIndex { std::string DebugInfo() const { u8 key_size = 16; - if (std::is_same_v) + if constexpr (std::is_same_v) key_size = 32; return fmt::format("key_size={:02X}, key={:02X}, field1={:016X}, field2={:016X}", key_size, static_cast(type), field1, field2); @@ -110,7 +111,7 @@ private: void AttemptLoadKeyFile(std::string_view dir1, std::string_view dir2, std::string_view filename, bool title); - static std::unordered_map> s128_file_id; - static std::unordered_map> s256_file_id; + const static std::unordered_map> s128_file_id; + const static std::unordered_map> s256_file_id; }; } // namespace Core::Crypto -- cgit v1.2.3 From 9d59b96ef907e8be2560107485f7616a2234c4a8 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sun, 29 Jul 2018 21:00:50 -0400 Subject: Use static const instead of const static --- src/core/crypto/key_manager.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/core/crypto/key_manager.h') diff --git a/src/core/crypto/key_manager.h b/src/core/crypto/key_manager.h index 28a560a3f..c09a6197e 100644 --- a/src/core/crypto/key_manager.h +++ b/src/core/crypto/key_manager.h @@ -111,7 +111,7 @@ private: void AttemptLoadKeyFile(std::string_view dir1, std::string_view dir2, std::string_view filename, bool title); - const static std::unordered_map> s128_file_id; - const static std::unordered_map> s256_file_id; + static const std::unordered_map> s128_file_id; + static const std::unordered_map> s256_file_id; }; } // namespace Core::Crypto -- cgit v1.2.3 From 187d8e215fb157edaa9f3976bebba9a9a7ed103d Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Mon, 30 Jul 2018 12:46:23 -0400 Subject: Use more descriptive error codes and messages --- src/core/crypto/key_manager.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/core/crypto/key_manager.h') diff --git a/src/core/crypto/key_manager.h b/src/core/crypto/key_manager.h index c09a6197e..03152a12c 100644 --- a/src/core/crypto/key_manager.h +++ b/src/core/crypto/key_manager.h @@ -102,6 +102,8 @@ public: void SetKey(S128KeyType id, Key128 key, u64 field1 = 0, u64 field2 = 0); void SetKey(S256KeyType id, Key256 key, u64 field1 = 0, u64 field2 = 0); + static bool KeyFileExists(bool title); + private: std::unordered_map, Key128> s128_keys; std::unordered_map, Key256> s256_keys; -- cgit v1.2.3