2019-12-09 11:57:37 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-12-09 11:57:37 +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/>.
|
|
|
|
*/
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
|
|
|
|
namespace ams::spl {
|
|
|
|
|
2020-05-11 22:04:51 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
enum class InitializeMode {
|
|
|
|
None,
|
|
|
|
General,
|
|
|
|
Crypto,
|
|
|
|
Ssl,
|
|
|
|
Es,
|
|
|
|
Fs,
|
|
|
|
Manu
|
|
|
|
};
|
|
|
|
|
2021-10-10 19:57:24 +00:00
|
|
|
constinit os::SdkMutex g_mutex;
|
|
|
|
constinit s32 g_initialize_count = 0;
|
|
|
|
constinit InitializeMode g_initialize_mode = InitializeMode::None;
|
2020-05-11 22:04:51 +00:00
|
|
|
|
|
|
|
Result AllocateAesKeySlotImpl(s32 *out) {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(serviceDispatchOut(splCryptoGetServiceSession(), 21, *out));
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result DeallocateAesKeySlotImpl(s32 slot) {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(serviceDispatchIn(splCryptoGetServiceSession(), 22, slot));
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 00:12:32 +00:00
|
|
|
Result GetAesKeySlotAvailableEventImpl(os::NativeHandle *out) {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(serviceDispatch(splCryptoGetServiceSession(), 23,
|
2020-05-11 22:04:51 +00:00
|
|
|
.out_handle_attrs = { SfOutHandleAttr_HipcCopy },
|
|
|
|
.out_handles = out,
|
2022-03-26 21:48:33 +00:00
|
|
|
));
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GetAesKeySlotAvailableEvent(os::SystemEvent *out) {
|
2021-10-05 00:12:32 +00:00
|
|
|
/* Get event handle. */
|
|
|
|
os::NativeHandle handle;
|
2020-05-11 22:04:51 +00:00
|
|
|
R_ABORT_UNLESS(GetAesKeySlotAvailableEventImpl(std::addressof(handle)));
|
|
|
|
|
|
|
|
/* Attach to event. */
|
|
|
|
out->AttachReadableHandle(handle, true, os::EventClearMode_ManualClear);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
Result WaitAvailableKeySlotAndExecute(F f) {
|
|
|
|
os::SystemEvent event;
|
|
|
|
auto is_event_initialized = false;
|
|
|
|
while (true) {
|
|
|
|
R_TRY_CATCH(static_cast<::ams::Result>(f())) {
|
2021-10-10 19:57:24 +00:00
|
|
|
R_CATCH(spl::ResultNoAvailableKeySlot) {
|
2020-05-11 22:04:51 +00:00
|
|
|
if (!is_event_initialized) {
|
|
|
|
GetAesKeySlotAvailableEvent(std::addressof(event));
|
|
|
|
is_event_initialized = true;
|
|
|
|
}
|
|
|
|
event.Wait();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} R_END_TRY_CATCH;
|
|
|
|
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
void Initialize(InitializeMode mode, F f) {
|
|
|
|
std::scoped_lock lk(g_mutex);
|
|
|
|
|
|
|
|
AMS_ASSERT(g_initialize_count >= 0);
|
|
|
|
AMS_ABORT_UNLESS(mode != InitializeMode::None);
|
|
|
|
|
|
|
|
if (g_initialize_count == 0) {
|
|
|
|
AMS_ABORT_UNLESS(g_initialize_mode == InitializeMode::None);
|
|
|
|
f();
|
|
|
|
g_initialize_mode = mode;
|
|
|
|
} else {
|
|
|
|
AMS_ABORT_UNLESS(g_initialize_mode == mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
++g_initialize_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Initialize() {
|
|
|
|
return Initialize(InitializeMode::General, [&]() {
|
|
|
|
R_ABORT_UNLESS(splInitialize());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitializeForCrypto() {
|
|
|
|
return Initialize(InitializeMode::Crypto, [&]() {
|
|
|
|
R_ABORT_UNLESS(splCryptoInitialize());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitializeForSsl() {
|
|
|
|
return Initialize(InitializeMode::Ssl, [&]() {
|
|
|
|
R_ABORT_UNLESS(splSslInitialize());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitializeForEs() {
|
|
|
|
return Initialize(InitializeMode::Es, [&]() {
|
|
|
|
R_ABORT_UNLESS(splEsInitialize());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitializeForFs() {
|
|
|
|
return Initialize(InitializeMode::Fs, [&]() {
|
|
|
|
R_ABORT_UNLESS(splFsInitialize());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitializeForManu() {
|
|
|
|
return Initialize(InitializeMode::Manu, [&]() {
|
|
|
|
R_ABORT_UNLESS(splManuInitialize());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Finalize() {
|
|
|
|
std::scoped_lock lk(g_mutex);
|
|
|
|
AMS_ASSERT(g_initialize_count > 0);
|
|
|
|
AMS_ABORT_UNLESS(g_initialize_mode != InitializeMode::None);
|
|
|
|
|
|
|
|
if ((--g_initialize_count) == 0) {
|
|
|
|
switch (g_initialize_mode) {
|
|
|
|
case InitializeMode::General: splExit(); break;
|
|
|
|
case InitializeMode::Crypto: splCryptoExit(); break;
|
|
|
|
case InitializeMode::Ssl: splSslExit(); break;
|
|
|
|
case InitializeMode::Es: splEsExit(); break;
|
|
|
|
case InitializeMode::Fs: splFsExit(); break;
|
|
|
|
case InitializeMode::Manu: splManuExit(); break;
|
|
|
|
AMS_UNREACHABLE_DEFAULT_CASE();
|
|
|
|
}
|
|
|
|
g_initialize_mode = InitializeMode::None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Result AllocateAesKeySlot(s32 *out_slot) {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(WaitAvailableKeySlotAndExecute([&]() -> Result {
|
|
|
|
R_RETURN(AllocateAesKeySlotImpl(out_slot));
|
|
|
|
}));
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result DeallocateAesKeySlot(s32 slot) {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(DeallocateAesKeySlotImpl(slot));
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GenerateAesKek(AccessKey *access_key, const void *key_source, size_t key_source_size, s32 generation, u32 option) {
|
|
|
|
AMS_ASSERT(key_source_size == sizeof(KeySource));
|
2021-10-06 22:20:48 +00:00
|
|
|
AMS_UNUSED(key_source_size);
|
|
|
|
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(splCryptoGenerateAesKek(key_source, generation, option, static_cast<void *>(access_key)));
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result LoadAesKey(s32 slot, const AccessKey &access_key, const void *key_source, size_t key_source_size) {
|
|
|
|
AMS_ASSERT(key_source_size == sizeof(KeySource));
|
2021-10-06 22:20:48 +00:00
|
|
|
AMS_UNUSED(key_source_size);
|
|
|
|
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(splCryptoLoadAesKey(std::addressof(access_key), key_source, static_cast<u32>(slot)));
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GenerateAesKey(void *dst, size_t dst_size, const AccessKey &access_key, const void *key_source, size_t key_source_size) {
|
|
|
|
AMS_ASSERT(dst_size >= crypto::AesEncryptor128::KeySize);
|
|
|
|
AMS_ASSERT(key_source_size == sizeof(KeySource));
|
2021-10-06 22:20:48 +00:00
|
|
|
AMS_UNUSED(dst_size, key_source_size);
|
|
|
|
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(WaitAvailableKeySlotAndExecute([&]() -> Result {
|
|
|
|
R_RETURN(splCryptoGenerateAesKey(std::addressof(access_key), key_source, dst));
|
|
|
|
}));
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GenerateSpecificAesKey(void *dst, size_t dst_size, const void *key_source, size_t key_source_size, s32 generation, u32 option) {
|
|
|
|
AMS_ASSERT(dst_size >= crypto::AesEncryptor128::KeySize);
|
|
|
|
AMS_ASSERT(key_source_size == sizeof(KeySource));
|
2021-10-06 22:20:48 +00:00
|
|
|
AMS_UNUSED(dst_size, key_source_size);
|
|
|
|
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(splFsGenerateSpecificAesKey(key_source, static_cast<u32>(generation), option, dst));
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result ComputeCtr(void *dst, size_t dst_size, s32 slot, const void *src, size_t src_size, const void *iv, size_t iv_size) {
|
|
|
|
AMS_ASSERT(iv_size >= 0x10);
|
|
|
|
AMS_ASSERT(dst_size >= src_size);
|
2021-10-06 22:20:48 +00:00
|
|
|
AMS_UNUSED(dst_size, iv_size);
|
2020-05-11 22:04:51 +00:00
|
|
|
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(splCryptoCryptAesCtr(src, dst, src_size, static_cast<s32>(slot), iv));
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result DecryptAesKey(void *dst, size_t dst_size, const void *key_source, size_t key_source_size, s32 generation, u32 option) {
|
|
|
|
AMS_ASSERT(dst_size >= crypto::AesEncryptor128::KeySize);
|
|
|
|
AMS_ASSERT(key_source_size == sizeof(KeySource));
|
2021-10-06 22:20:48 +00:00
|
|
|
AMS_UNUSED(dst_size, key_source_size);
|
|
|
|
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(WaitAvailableKeySlotAndExecute([&]() -> Result {
|
|
|
|
R_RETURN(splCryptoDecryptAesKey(key_source, static_cast<u32>(generation), option, dst));
|
|
|
|
}));
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetConfig(u64 *out, ConfigItem item) {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(splGetConfig(static_cast<::SplConfigItem>(item), out));
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 07:54:17 +00:00
|
|
|
Result SetConfig(ConfigItem item, u64 v) {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(splSetConfig(static_cast<::SplConfigItem>(item), v));
|
2021-10-11 07:54:17 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 22:04:51 +00:00
|
|
|
bool IsDevelopment() {
|
|
|
|
bool is_dev;
|
|
|
|
R_ABORT_UNLESS(splIsDevelopment(std::addressof(is_dev)));
|
|
|
|
return is_dev;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MemoryArrangement GetMemoryArrangement() {
|
2020-05-11 22:04:51 +00:00
|
|
|
u64 mode = 0;
|
|
|
|
R_ABORT_UNLESS(spl::GetConfig(std::addressof(mode), spl::ConfigItem::MemoryMode));
|
|
|
|
switch (mode & 0x3F) {
|
2019-12-09 11:57:37 +00:00
|
|
|
case 2:
|
|
|
|
return MemoryArrangement_StandardForAppletDev;
|
|
|
|
case 3:
|
|
|
|
return MemoryArrangement_StandardForSystemDev;
|
|
|
|
case 17:
|
|
|
|
return MemoryArrangement_Expanded;
|
|
|
|
case 18:
|
|
|
|
return MemoryArrangement_ExpandedForAppletDev;
|
|
|
|
default:
|
|
|
|
return MemoryArrangement_Standard;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 22:04:51 +00:00
|
|
|
Result SetBootReason(BootReasonValue boot_reason) {
|
|
|
|
static_assert(sizeof(boot_reason) == sizeof(u32));
|
2020-04-14 09:54:55 +00:00
|
|
|
|
2020-05-11 22:04:51 +00:00
|
|
|
u32 v;
|
|
|
|
std::memcpy(std::addressof(v), std::addressof(boot_reason), sizeof(v));
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(splSetBootReason(v));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 22:04:51 +00:00
|
|
|
Result GetBootReason(BootReasonValue *out) {
|
|
|
|
static_assert(sizeof(*out) == sizeof(u32));
|
|
|
|
|
|
|
|
u32 v;
|
|
|
|
R_TRY(splGetBootReason(std::addressof(v)));
|
|
|
|
|
|
|
|
std::memcpy(out, std::addressof(v), sizeof(*out));
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 22:04:51 +00:00
|
|
|
SocType GetSocType() {
|
|
|
|
switch (GetHardwareType()) {
|
2019-12-09 11:57:37 +00:00
|
|
|
case HardwareType::Icosa:
|
|
|
|
case HardwareType::Copper:
|
2020-05-11 22:04:51 +00:00
|
|
|
return SocType_Erista;
|
2019-12-09 11:57:37 +00:00
|
|
|
case HardwareType::Hoag:
|
|
|
|
case HardwareType::Iowa:
|
2020-12-28 23:41:21 +00:00
|
|
|
case HardwareType::Aula:
|
2020-05-11 22:04:51 +00:00
|
|
|
return SocType_Mariko;
|
2019-12-09 11:57:37 +00:00
|
|
|
AMS_UNREACHABLE_DEFAULT_CASE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 22:04:51 +00:00
|
|
|
Result GetPackage2Hash(void *dst, size_t dst_size) {
|
|
|
|
AMS_ASSERT(dst_size >= crypto::Sha256Generator::HashSize);
|
2021-10-06 22:20:48 +00:00
|
|
|
AMS_UNUSED(dst_size);
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(splFsGetPackage2Hash(dst));
|
2020-04-22 23:22:14 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 22:04:51 +00:00
|
|
|
Result GenerateRandomBytes(void *out, size_t buffer_size) {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(splGetRandomBytes(out, buffer_size));
|
2020-05-11 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result LoadPreparedAesKey(s32 slot, const AccessKey &access_key) {
|
|
|
|
if (g_initialize_mode == InitializeMode::Fs) {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(splFsLoadTitlekey(std::addressof(access_key), static_cast<u32>(slot)));
|
2020-05-11 22:04:51 +00:00
|
|
|
} else {
|
|
|
|
/* TODO: libnx binding not available. */
|
2022-03-26 21:48:33 +00:00
|
|
|
/* R_RETURN(splEsLoadTitlekey(std::addressof(access_key), static_cast<u32>(slot))); */
|
2020-05-11 22:04:51 +00:00
|
|
|
AMS_ABORT_UNLESS(false);
|
|
|
|
}
|
2020-04-22 23:22:14 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|