2020-05-11 22:04:51 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-05-11 22:04:51 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
|
2021-10-10 19:57:24 +00:00
|
|
|
namespace ams::spl::impl {
|
2020-05-11 22:04:51 +00:00
|
|
|
|
2021-10-10 19:57:24 +00:00
|
|
|
class AesKeySlotCacheEntry : public util::IntrusiveListBaseNode<AesKeySlotCacheEntry> {
|
|
|
|
NON_COPYABLE(AesKeySlotCacheEntry);
|
|
|
|
NON_MOVEABLE(AesKeySlotCacheEntry);
|
2020-05-11 22:04:51 +00:00
|
|
|
private:
|
2021-10-10 19:57:24 +00:00
|
|
|
friend class AesKeySlotCache;
|
2020-05-11 22:04:51 +00:00
|
|
|
public:
|
|
|
|
static constexpr size_t KeySize = crypto::AesDecryptor128::KeySize;
|
|
|
|
private:
|
2021-10-10 19:57:24 +00:00
|
|
|
const s32 m_aes_keyslot_index;
|
|
|
|
s32 m_virtual_aes_keyslot;
|
2020-05-11 22:04:51 +00:00
|
|
|
public:
|
2021-10-10 19:57:24 +00:00
|
|
|
explicit AesKeySlotCacheEntry(s32 idx) : m_aes_keyslot_index(idx), m_virtual_aes_keyslot(-1) { /* ... */ }
|
2020-05-11 22:04:51 +00:00
|
|
|
|
|
|
|
bool Contains(s32 virtual_slot) const {
|
2021-10-10 19:57:24 +00:00
|
|
|
return virtual_slot == m_virtual_aes_keyslot;
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 19:57:24 +00:00
|
|
|
s32 GetPhysicalAesKeySlotIndex() const { return m_aes_keyslot_index; }
|
2020-05-11 22:04:51 +00:00
|
|
|
|
2021-10-10 19:57:24 +00:00
|
|
|
s32 GetVirtualAesKeySlotIndex() const { return m_virtual_aes_keyslot; }
|
2020-05-11 22:04:51 +00:00
|
|
|
|
2021-10-10 19:57:24 +00:00
|
|
|
void SetVirtualAesKeySlot(s32 virtual_slot) {
|
|
|
|
m_virtual_aes_keyslot = virtual_slot;
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 19:57:24 +00:00
|
|
|
void ClearVirtualAesKeySlot() {
|
|
|
|
m_virtual_aes_keyslot = -1;
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-10 19:57:24 +00:00
|
|
|
class AesKeySlotCache {
|
|
|
|
NON_COPYABLE(AesKeySlotCache);
|
|
|
|
NON_MOVEABLE(AesKeySlotCache);
|
2020-05-11 22:04:51 +00:00
|
|
|
private:
|
2021-10-10 19:57:24 +00:00
|
|
|
using AesKeySlotCacheEntryList = util::IntrusiveListBaseTraits<AesKeySlotCacheEntry>::ListType;
|
2020-05-11 22:04:51 +00:00
|
|
|
private:
|
2021-10-10 19:57:24 +00:00
|
|
|
AesKeySlotCacheEntryList m_mru_list;
|
2020-05-11 22:04:51 +00:00
|
|
|
public:
|
2021-10-10 19:57:24 +00:00
|
|
|
constexpr AesKeySlotCache() : m_mru_list() { /* ... */ }
|
2020-05-11 22:04:51 +00:00
|
|
|
|
|
|
|
s32 Allocate(s32 virtual_slot) {
|
|
|
|
return this->AllocateFromLru(virtual_slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Find(s32 *out, s32 virtual_slot) {
|
2021-10-10 07:14:06 +00:00
|
|
|
for (auto it = m_mru_list.begin(); it != m_mru_list.end(); ++it) {
|
2020-05-11 22:04:51 +00:00
|
|
|
if (it->Contains(virtual_slot)) {
|
2021-10-10 19:57:24 +00:00
|
|
|
*out = it->GetPhysicalAesKeySlotIndex();
|
2020-05-11 22:04:51 +00:00
|
|
|
|
|
|
|
this->UpdateMru(it);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Release(s32 *out, s32 virtual_slot) {
|
2021-10-10 07:14:06 +00:00
|
|
|
for (auto it = m_mru_list.begin(); it != m_mru_list.end(); ++it) {
|
2020-05-11 22:04:51 +00:00
|
|
|
if (it->Contains(virtual_slot)) {
|
2021-10-10 19:57:24 +00:00
|
|
|
*out = it->GetPhysicalAesKeySlotIndex();
|
|
|
|
it->ClearVirtualAesKeySlot();
|
2020-05-11 22:04:51 +00:00
|
|
|
|
|
|
|
this->UpdateLru(it);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindPhysical(s32 physical_slot) {
|
2021-10-10 07:14:06 +00:00
|
|
|
for (auto it = m_mru_list.begin(); it != m_mru_list.end(); ++it) {
|
2021-10-10 19:57:24 +00:00
|
|
|
if (it->GetPhysicalAesKeySlotIndex() == physical_slot) {
|
2020-05-11 22:04:51 +00:00
|
|
|
this->UpdateMru(it);
|
|
|
|
|
2021-10-10 19:57:24 +00:00
|
|
|
if (it->GetVirtualAesKeySlotIndex() == physical_slot) {
|
2020-05-11 22:04:51 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
2021-10-10 19:57:24 +00:00
|
|
|
it->SetVirtualAesKeySlot(physical_slot);
|
2020-05-11 22:04:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AMS_ABORT();
|
|
|
|
}
|
|
|
|
|
2021-10-10 19:57:24 +00:00
|
|
|
void AddEntry(AesKeySlotCacheEntry *entry) {
|
2021-10-10 07:14:06 +00:00
|
|
|
m_mru_list.push_front(*entry);
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
s32 AllocateFromLru(s32 virtual_slot) {
|
2021-10-10 07:14:06 +00:00
|
|
|
AMS_ASSERT(!m_mru_list.empty());
|
2020-05-11 22:04:51 +00:00
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
auto it = m_mru_list.rbegin();
|
2021-10-10 19:57:24 +00:00
|
|
|
it->SetVirtualAesKeySlot(virtual_slot);
|
2020-05-11 22:04:51 +00:00
|
|
|
|
|
|
|
auto *entry = std::addressof(*it);
|
2021-10-10 07:14:06 +00:00
|
|
|
m_mru_list.pop_back();
|
|
|
|
m_mru_list.push_front(*entry);
|
2020-05-11 22:04:51 +00:00
|
|
|
|
2021-10-10 19:57:24 +00:00
|
|
|
return entry->GetPhysicalAesKeySlotIndex();
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 19:57:24 +00:00
|
|
|
void UpdateMru(AesKeySlotCacheEntryList::iterator it) {
|
2020-05-11 22:04:51 +00:00
|
|
|
auto *entry = std::addressof(*it);
|
2021-10-10 07:14:06 +00:00
|
|
|
m_mru_list.erase(it);
|
|
|
|
m_mru_list.push_front(*entry);
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 19:57:24 +00:00
|
|
|
void UpdateLru(AesKeySlotCacheEntryList::iterator it) {
|
2020-05-11 22:04:51 +00:00
|
|
|
auto *entry = std::addressof(*it);
|
2021-10-10 07:14:06 +00:00
|
|
|
m_mru_list.erase(it);
|
|
|
|
m_mru_list.push_back(*entry);
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|