Merge pull request #4181 from wwylele/cia-crypto

Add encrypted CIA support
This commit is contained in:
Weiyi Wang 2018-10-01 14:23:35 -04:00 committed by GitHub
commit 5fb3137bdc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 272 additions and 51 deletions

View file

@ -25,26 +25,26 @@ struct KeySlot {
boost::optional<AESKey> y;
boost::optional<AESKey> normal;
void SetKeyX(const AESKey& key) {
void SetKeyX(boost::optional<AESKey> key) {
x = key;
if (y && generator_constant) {
GenerateNormalKey();
}
GenerateNormalKey();
}
void SetKeyY(const AESKey& key) {
void SetKeyY(boost::optional<AESKey> key) {
y = key;
if (x && generator_constant) {
GenerateNormalKey();
}
GenerateNormalKey();
}
void SetNormalKey(const AESKey& key) {
void SetNormalKey(boost::optional<AESKey> key) {
normal = key;
}
void GenerateNormalKey() {
normal = Lrot128(Add128(Xor128(Lrot128(*x, 2), *y), *generator_constant), 87);
if (x && y && generator_constant) {
normal = Lrot128(Add128(Xor128(Lrot128(*x, 2), *y), *generator_constant), 87);
} else {
normal = boost::none;
}
}
void Clear() {
@ -55,6 +55,7 @@ struct KeySlot {
};
std::array<KeySlot, KeySlotID::MaxKeySlotID> key_slots;
std::array<boost::optional<AESKey>, 6> common_key_y_slots;
AESKey HexToKey(const std::string& hex) {
if (hex.size() < 32) {
@ -102,6 +103,16 @@ void LoadPresetKeys() {
continue;
}
std::size_t common_key_index;
if (std::sscanf(name.c_str(), "common%zd", &common_key_index) == 1) {
if (common_key_index >= common_key_y_slots.size()) {
LOG_ERROR(HW_AES, "Invalid common key index {}", common_key_index);
} else {
common_key_y_slots[common_key_index] = key;
}
continue;
}
std::size_t slot_id;
char key_type;
if (std::sscanf(name.c_str(), "slot0x%zXKey%c", &slot_id, &key_type) != 2) {
@ -165,5 +176,9 @@ AESKey GetNormalKey(std::size_t slot_id) {
return key_slots.at(slot_id).normal.value_or(AESKey{});
}
void SelectCommonKeyIndex(u8 index) {
key_slots[KeySlotID::TicketCommonKey].SetKeyY(common_key_y_slots.at(index));
}
} // namespace AES
} // namespace HW