diff options
Diffstat (limited to 'src/core/hw/aes/key.cpp')
| -rw-r--r-- | src/core/hw/aes/key.cpp | 173 |
1 files changed, 0 insertions, 173 deletions
diff --git a/src/core/hw/aes/key.cpp b/src/core/hw/aes/key.cpp deleted file mode 100644 index 4e8a8a59a..000000000 --- a/src/core/hw/aes/key.cpp +++ /dev/null | |||
| @@ -1,173 +0,0 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <exception> | ||
| 7 | #include <sstream> | ||
| 8 | #include <boost/optional.hpp> | ||
| 9 | #include "common/common_paths.h" | ||
| 10 | #include "common/file_util.h" | ||
| 11 | #include "common/logging/log.h" | ||
| 12 | #include "common/string_util.h" | ||
| 13 | #include "core/hw/aes/arithmetic128.h" | ||
| 14 | #include "core/hw/aes/key.h" | ||
| 15 | |||
| 16 | namespace HW { | ||
| 17 | namespace AES { | ||
| 18 | |||
| 19 | namespace { | ||
| 20 | |||
| 21 | boost::optional<AESKey> generator_constant; | ||
| 22 | |||
| 23 | struct KeySlot { | ||
| 24 | boost::optional<AESKey> x; | ||
| 25 | boost::optional<AESKey> y; | ||
| 26 | boost::optional<AESKey> normal; | ||
| 27 | |||
| 28 | void SetKeyX(const AESKey& key) { | ||
| 29 | x = key; | ||
| 30 | if (y && generator_constant) { | ||
| 31 | GenerateNormalKey(); | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | void SetKeyY(const AESKey& key) { | ||
| 36 | y = key; | ||
| 37 | if (x && generator_constant) { | ||
| 38 | GenerateNormalKey(); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | void SetNormalKey(const AESKey& key) { | ||
| 43 | normal = key; | ||
| 44 | } | ||
| 45 | |||
| 46 | void GenerateNormalKey() { | ||
| 47 | normal = Lrot128(Add128(Xor128(Lrot128(*x, 2), *y), *generator_constant), 87); | ||
| 48 | } | ||
| 49 | |||
| 50 | void Clear() { | ||
| 51 | x.reset(); | ||
| 52 | y.reset(); | ||
| 53 | normal.reset(); | ||
| 54 | } | ||
| 55 | }; | ||
| 56 | |||
| 57 | std::array<KeySlot, KeySlotID::MaxKeySlotID> key_slots; | ||
| 58 | |||
| 59 | void ClearAllKeys() { | ||
| 60 | for (KeySlot& slot : key_slots) { | ||
| 61 | slot.Clear(); | ||
| 62 | } | ||
| 63 | generator_constant.reset(); | ||
| 64 | } | ||
| 65 | |||
| 66 | AESKey HexToKey(const std::string& hex) { | ||
| 67 | if (hex.size() < 32) { | ||
| 68 | throw std::invalid_argument("hex string is too short"); | ||
| 69 | } | ||
| 70 | |||
| 71 | AESKey key; | ||
| 72 | for (size_t i = 0; i < key.size(); ++i) { | ||
| 73 | key[i] = static_cast<u8>(std::stoi(hex.substr(i * 2, 2), 0, 16)); | ||
| 74 | } | ||
| 75 | |||
| 76 | return key; | ||
| 77 | } | ||
| 78 | |||
| 79 | void LoadPresetKeys() { | ||
| 80 | const std::string filepath = FileUtil::GetUserPath(D_SYSDATA_IDX) + AES_KEYS; | ||
| 81 | FileUtil::CreateFullPath(filepath); // Create path if not already created | ||
| 82 | std::ifstream file; | ||
| 83 | OpenFStream(file, filepath, std::ios_base::in); | ||
| 84 | if (!file) { | ||
| 85 | return; | ||
| 86 | } | ||
| 87 | |||
| 88 | while (!file.eof()) { | ||
| 89 | std::string line; | ||
| 90 | std::getline(file, line); | ||
| 91 | std::vector<std::string> parts; | ||
| 92 | Common::SplitString(line, '=', parts); | ||
| 93 | if (parts.size() != 2) { | ||
| 94 | LOG_ERROR(HW_AES, "Failed to parse %s", line.c_str()); | ||
| 95 | continue; | ||
| 96 | } | ||
| 97 | |||
| 98 | const std::string& name = parts[0]; | ||
| 99 | AESKey key; | ||
| 100 | try { | ||
| 101 | key = HexToKey(parts[1]); | ||
| 102 | } catch (const std::logic_error& e) { | ||
| 103 | LOG_ERROR(HW_AES, "Invalid key %s: %s", parts[1].c_str(), e.what()); | ||
| 104 | continue; | ||
| 105 | } | ||
| 106 | |||
| 107 | if (name == "generator") { | ||
| 108 | generator_constant = key; | ||
| 109 | continue; | ||
| 110 | } | ||
| 111 | |||
| 112 | size_t slot_id; | ||
| 113 | char key_type; | ||
| 114 | if (std::sscanf(name.c_str(), "slot0x%zXKey%c", &slot_id, &key_type) != 2) { | ||
| 115 | LOG_ERROR(HW_AES, "Invalid key name %s", name.c_str()); | ||
| 116 | continue; | ||
| 117 | } | ||
| 118 | |||
| 119 | if (slot_id >= MaxKeySlotID) { | ||
| 120 | LOG_ERROR(HW_AES, "Out of range slot ID 0x%zX", slot_id); | ||
| 121 | continue; | ||
| 122 | } | ||
| 123 | |||
| 124 | switch (key_type) { | ||
| 125 | case 'X': | ||
| 126 | key_slots.at(slot_id).SetKeyX(key); | ||
| 127 | break; | ||
| 128 | case 'Y': | ||
| 129 | key_slots.at(slot_id).SetKeyY(key); | ||
| 130 | break; | ||
| 131 | case 'N': | ||
| 132 | key_slots.at(slot_id).SetNormalKey(key); | ||
| 133 | break; | ||
| 134 | default: | ||
| 135 | LOG_ERROR(HW_AES, "Invalid key type %c", key_type); | ||
| 136 | break; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | } // namespace | ||
| 142 | |||
| 143 | void InitKeys() { | ||
| 144 | ClearAllKeys(); | ||
| 145 | LoadPresetKeys(); | ||
| 146 | } | ||
| 147 | |||
| 148 | void SetGeneratorConstant(const AESKey& key) { | ||
| 149 | generator_constant = key; | ||
| 150 | } | ||
| 151 | |||
| 152 | void SetKeyX(size_t slot_id, const AESKey& key) { | ||
| 153 | key_slots.at(slot_id).SetKeyX(key); | ||
| 154 | } | ||
| 155 | |||
| 156 | void SetKeyY(size_t slot_id, const AESKey& key) { | ||
| 157 | key_slots.at(slot_id).SetKeyY(key); | ||
| 158 | } | ||
| 159 | |||
| 160 | void SetNormalKey(size_t slot_id, const AESKey& key) { | ||
| 161 | key_slots.at(slot_id).SetNormalKey(key); | ||
| 162 | } | ||
| 163 | |||
| 164 | bool IsNormalKeyAvailable(size_t slot_id) { | ||
| 165 | return key_slots.at(slot_id).normal.is_initialized(); | ||
| 166 | } | ||
| 167 | |||
| 168 | AESKey GetNormalKey(size_t slot_id) { | ||
| 169 | return key_slots.at(slot_id).normal.value_or(AESKey{}); | ||
| 170 | } | ||
| 171 | |||
| 172 | } // namespace AES | ||
| 173 | } // namespace HW | ||