summaryrefslogtreecommitdiff
path: root/src/core/crypto/key_manager.h
diff options
context:
space:
mode:
authorGravatar Zach Hilman2018-07-28 21:39:42 -0400
committerGravatar Zach Hilman2018-08-01 00:16:54 -0400
commit239a3113e4c6a53a2c7b12e67a0f21afae24b0aa (patch)
tree027bc4288f08be240d0b9b2a5f6c6431e76b8b4f /src/core/crypto/key_manager.h
parentExtract mbedtls to cpp file (diff)
downloadyuzu-239a3113e4c6a53a2c7b12e67a0f21afae24b0aa.tar.gz
yuzu-239a3113e4c6a53a2c7b12e67a0f21afae24b0aa.tar.xz
yuzu-239a3113e4c6a53a2c7b12e67a0f21afae24b0aa.zip
Make XCI comply to review and style guidelines
Diffstat (limited to 'src/core/crypto/key_manager.h')
-rw-r--r--src/core/crypto/key_manager.h77
1 files changed, 37 insertions, 40 deletions
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 @@
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#pragma once 5#pragma once
6
6#include <array> 7#include <array>
7#include <unordered_map> 8#include <unordered_map>
8#include <vector> 9#include <vector>
9#include <fmt/format.h> 10#include <fmt/format.h>
10#include "common/common_types.h" 11#include "common/common_types.h"
11 12
12namespace Crypto { 13namespace Core::Crypto {
13 14
14typedef std::array<u8, 0x10> Key128; 15using Key128 = std::array<u8, 0x10>;
15typedef std::array<u8, 0x20> Key256; 16using Key256 = std::array<u8, 0x20>;
16typedef std::array<u8, 0x20> SHA256Hash; 17using SHA256Hash = std::array<u8, 0x20>;
17 18
18static_assert(sizeof(Key128) == 16, "Key128 must be 128 bytes big."); 19static_assert(sizeof(Key128) == 16, "Key128 must be 128 bytes big.");
19static_assert(sizeof(Key256) == 32, "Key128 must be 128 bytes big."); 20static_assert(sizeof(Key256) == 32, "Key128 must be 128 bytes big.");
20 21
21enum class S256KeyType : u64 { 22enum class S256KeyType : u64 {
22 HEADER, // 23 Header, //
23 SD_SAVE, // 24 SDSave, //
24 SD_NCA, // 25 SDNCA, //
25}; 26};
26 27
27enum class S128KeyType : u64 { 28enum class S128KeyType : u64 {
28 MASTER, // f1=crypto revision 29 Master, // f1=crypto revision
29 PACKAGE1, // f1=crypto revision 30 Package1, // f1=crypto revision
30 PACKAGE2, // f1=crypto revision 31 Package2, // f1=crypto revision
31 TITLEKEK, // f1=crypto revision 32 Titlekek, // f1=crypto revision
32 ETICKET_RSA_KEK, // 33 ETicketRSAKek, //
33 KEY_AREA, // f1=crypto revision f2=type {app, ocean, system} 34 KeyArea, // f1=crypto revision f2=type {app, ocean, system}
34 SD_SEED, // 35 SDSeed, //
35 TITLEKEY, // f1=rights id LSB f2=rights id MSB 36 Titlekey, // f1=rights id LSB f2=rights id MSB
36}; 37};
37 38
38enum class KeyAreaKeyType : u8 { 39enum class KeyAreaKeyType : u8 {
@@ -47,7 +48,7 @@ struct KeyIndex {
47 u64 field1; 48 u64 field1;
48 u64 field2; 49 u64 field2;
49 50
50 std::string DebugInfo() { 51 std::string DebugInfo() const {
51 u8 key_size = 16; 52 u8 key_size = 16;
52 if (std::is_same_v<KeyType, S256KeyType>) 53 if (std::is_same_v<KeyType, S256KeyType>)
53 key_size = 32; 54 key_size = 32;
@@ -60,15 +61,20 @@ struct KeyIndex {
60 61
61template <typename KeyType> 62template <typename KeyType>
62bool operator==(const KeyIndex<KeyType>& lhs, const KeyIndex<KeyType>& rhs) { 63bool operator==(const KeyIndex<KeyType>& lhs, const KeyIndex<KeyType>& rhs) {
63 return lhs.type == rhs.type && lhs.field1 == rhs.field1 && lhs.field2 == rhs.field2; 64 return std::tie(lhs.type, lhs.field1, lhs.field2) == std::tie(rhs.type, rhs.field1, rhs.field2);
64} 65}
65 66
66} // namespace Crypto 67template <typename KeyType>
68bool operator!=(const KeyIndex<KeyType>& lhs, const KeyIndex<KeyType>& rhs) {
69 return !operator==(lhs, rhs);
70}
71
72} // namespace Core::Crypto
67 73
68namespace std { 74namespace std {
69template <typename KeyType> 75template <typename KeyType>
70struct hash<Crypto::KeyIndex<KeyType>> { 76struct hash<Core::Crypto::KeyIndex<KeyType>> {
71 size_t operator()(const Crypto::KeyIndex<KeyType>& k) const { 77 size_t operator()(const Core::Crypto::KeyIndex<KeyType>& k) const {
72 using std::hash; 78 using std::hash;
73 79
74 return ((hash<u64>()(static_cast<u64>(k.type)) ^ (hash<u64>()(k.field1) << 1)) >> 1) ^ 80 return ((hash<u64>()(static_cast<u64>(k.type)) ^ (hash<u64>()(k.field1) << 1)) >> 1) ^
@@ -77,41 +83,32 @@ struct hash<Crypto::KeyIndex<KeyType>> {
77}; 83};
78} // namespace std 84} // namespace std
79 85
80namespace Crypto { 86namespace Core::Crypto {
81 87
82std::array<u8, 0x10> operator"" _array16(const char* str, size_t len); 88std::array<u8, 0x10> operator"" _array16(const char* str, size_t len);
83std::array<u8, 0x20> operator"" _array32(const char* str, size_t len); 89std::array<u8, 0x20> operator"" _array32(const char* str, size_t len);
84 90
85struct KeyManager { 91class KeyManager {
86 void SetValidationMode(bool dev); 92public:
87 void LoadFromFile(std::string_view filename, bool is_title_keys); 93 KeyManager();
88 94
89 bool HasKey(S128KeyType id, u64 field1 = 0, u64 field2 = 0); 95 bool HasKey(S128KeyType id, u64 field1 = 0, u64 field2 = 0) const;
90 bool HasKey(S256KeyType id, u64 field1 = 0, u64 field2 = 0); 96 bool HasKey(S256KeyType id, u64 field1 = 0, u64 field2 = 0) const;
91 97
92 Key128 GetKey(S128KeyType id, u64 field1 = 0, u64 field2 = 0); 98 Key128 GetKey(S128KeyType id, u64 field1 = 0, u64 field2 = 0) const;
93 Key256 GetKey(S256KeyType id, u64 field1 = 0, u64 field2 = 0); 99 Key256 GetKey(S256KeyType id, u64 field1 = 0, u64 field2 = 0) const;
94 100
95 void SetKey(S128KeyType id, Key128 key, u64 field1 = 0, u64 field2 = 0); 101 void SetKey(S128KeyType id, Key128 key, u64 field1 = 0, u64 field2 = 0);
96 void SetKey(S256KeyType id, Key256 key, u64 field1 = 0, u64 field2 = 0); 102 void SetKey(S256KeyType id, Key256 key, u64 field1 = 0, u64 field2 = 0);
97 103
98 bool ValidateKey(S128KeyType key, u64 field1 = 0, u64 field2 = 0);
99 bool ValidateKey(S256KeyType key, u64 field1 = 0, u64 field2 = 0);
100
101private: 104private:
102 std::unordered_map<KeyIndex<S128KeyType>, Key128> s128_keys; 105 std::unordered_map<KeyIndex<S128KeyType>, Key128> s128_keys;
103 std::unordered_map<KeyIndex<S256KeyType>, Key256> s256_keys; 106 std::unordered_map<KeyIndex<S256KeyType>, Key256> s256_keys;
104 107
105 bool dev_mode = false; 108 bool dev_mode;
109 void LoadFromFile(std::string_view filename, bool is_title_keys);
106 110
107 static std::unordered_map<KeyIndex<S128KeyType>, SHA256Hash> s128_hash_prod;
108 static std::unordered_map<KeyIndex<S256KeyType>, SHA256Hash> s256_hash_prod;
109 static std::unordered_map<KeyIndex<S128KeyType>, SHA256Hash> s128_hash_dev;
110 static std::unordered_map<KeyIndex<S256KeyType>, SHA256Hash> s256_hash_dev;
111 static std::unordered_map<std::string, KeyIndex<S128KeyType>> s128_file_id; 111 static std::unordered_map<std::string, KeyIndex<S128KeyType>> s128_file_id;
112 static std::unordered_map<std::string, KeyIndex<S256KeyType>> s256_file_id; 112 static std::unordered_map<std::string, KeyIndex<S256KeyType>> s256_file_id;
113}; 113};
114 114} // namespace Core::Crypto
115extern KeyManager keys;
116
117} // namespace Crypto