spl: refactor for accuracy/move into libstrat

This commit is contained in:
Michael Scire 2021-10-10 12:57:24 -07:00
parent 4758dfa933
commit d8a36e39f2
40 changed files with 1898 additions and 1732 deletions

View file

@ -18,7 +18,8 @@
#include <stratosphere/spl/spl_types.hpp>
#include <stratosphere/spl/spl_api.hpp>
#include <stratosphere/spl/smc/spl_smc.hpp>
#include <stratosphere/spl/smc/spl_secure_monitor_api.hpp>
#include <stratosphere/spl/impl/spl_api_impl.hpp>
#include <stratosphere/spl/impl/spl_random_interface.hpp>
#include <stratosphere/spl/impl/spl_deprecated_general_interface.hpp>
#include <stratosphere/spl/impl/spl_general_interface.hpp>

View file

@ -0,0 +1,84 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/spl/spl_types.hpp>
namespace ams::spl::impl {
constexpr inline s32 AesKeySlotMin = 16;
constexpr inline s32 AesKeySlotCount = 9;
constexpr inline s32 AesKeySlotMax = AesKeySlotMin + AesKeySlotCount - 1;
/* Initialization. */
void Initialize();
/* General. */
Result GetConfig(u64 *out, spl::ConfigItem key);
Result ModularExponentiate(void *out, size_t out_size, const void *base, size_t base_size, const void *exp, size_t exp_size, const void *mod, size_t mod_size);
Result SetConfig(spl::ConfigItem key, u64 value);
Result GenerateRandomBytes(void *out, size_t size);
Result IsDevelopment(bool *out);
Result SetBootReason(BootReasonValue boot_reason);
Result GetBootReason(BootReasonValue *out);
ALWAYS_INLINE bool GetConfigBool(spl::ConfigItem key) {
u64 v;
R_ABORT_UNLESS(::ams::spl::impl::GetConfig(std::addressof(v), key));
return v != 0;
}
/* Crypto. */
Result GenerateAesKek(AccessKey *out_access_key, const KeySource &key_source, u32 generation, u32 option);
Result LoadAesKey(s32 keyslot, const AccessKey &access_key, const KeySource &key_source);
Result GenerateAesKey(AesKey *out_key, const AccessKey &access_key, const KeySource &key_source);
Result DecryptAesKey(AesKey *out_key, const KeySource &key_source, u32 generation, u32 option);
Result ComputeCtr(void *dst, size_t dst_size, s32 keyslot, const void *src, size_t src_size, const IvCtr &iv_ctr);
Result ComputeCmac(Cmac *out_cmac, s32 keyslot, const void *data, size_t size);
Result AllocateAesKeySlot(s32 *out_keyslot);
Result DeallocateAesKeySlot(s32 keyslot);
Result TestAesKeySlot(s32 *out_index, s32 keyslot);
os::SystemEvent *GetAesKeySlotAvailableEvent();
/* RSA. */
Result DecryptDeviceUniqueData(void *dst, size_t dst_size, const void *src, size_t src_size, const AccessKey &access_key, const KeySource &key_source, u32 option);
/* SSL */
Result DecryptAndStoreSslClientCertKey(const void *src, size_t src_size, const AccessKey &access_key, const KeySource &key_source);
Result ModularExponentiateWithSslClientCertKey(void *out, size_t out_size, const void *base, size_t base_size, const void *mod, size_t mod_size);
/* ES */
Result LoadEsDeviceKey(const void *src, size_t src_size, const AccessKey &access_key, const KeySource &key_source, u32 option);
Result PrepareEsTitleKey(AccessKey *out_access_key, const void *base, size_t base_size, const void *mod, size_t mod_size, const void *label_digest, size_t label_digest_size, u32 generation);
Result PrepareCommonEsTitleKey(AccessKey *out_access_key, const KeySource &key_source, u32 generation);
Result DecryptAndStoreDrmDeviceCertKey(const void *src, size_t src_size, const AccessKey &access_key, const KeySource &key_source);
Result ModularExponentiateWithDrmDeviceCertKey(void *out, size_t out_size, const void *base, size_t base_size, const void *mod, size_t mod_size);
Result PrepareEsArchiveKey(AccessKey *out_access_key, const void *base, size_t base_size, const void *mod, size_t mod_size, const void *label_digest, size_t label_digest_size, u32 generation);
Result LoadPreparedAesKey(s32 keyslot, const AccessKey &access_key);
/* FS */
Result DecryptAndStoreGcKey(const void *src, size_t src_size, const AccessKey &access_key, const KeySource &key_source, u32 option);
Result DecryptGcMessage(u32 *out_size, void *dst, size_t dst_size, const void *base, size_t base_size, const void *mod, size_t mod_size, const void *label_digest, size_t label_digest_size);
Result GenerateSpecificAesKey(AesKey *out_key, const KeySource &key_source, u32 generation, u32 which);
Result LoadPreparedAesKey(s32 keyslot, const AccessKey &access_key);
Result GetPackage2Hash(void *dst, const size_t size);
/* Manu. */
Result ReencryptDeviceUniqueData(void *dst, size_t dst_size, const void *src, size_t src_size, const AccessKey &access_key_dec, const KeySource &source_dec, const AccessKey &access_key_enc, const KeySource &source_enc, u32 option);
}

View file

@ -20,24 +20,24 @@
namespace ams::spl::smc {
/* Helpers for converting arguments. */
inline u32 GetComputeAesMode(CipherMode mode, u32 keyslot) {
constexpr ALWAYS_INLINE u32 GetComputeAesMode(CipherMode mode, u32 keyslot) {
return static_cast<u32>((static_cast<u32>(mode) << 4) | (keyslot & 7));
}
inline u32 GetPrepareEsDeviceUniqueKeyOption(EsCommonKeyType type, u32 generation) {
constexpr ALWAYS_INLINE u32 GetPrepareEsDeviceUniqueKeyOption(EsDeviceUniqueKeyType type, u32 generation) {
return static_cast<u32>((static_cast<u32>(type) << 6) | (generation & 0x3F));
}
/* Functions. */
Result SetConfig(spl::ConfigItem which, const void *address, const u64 *value, size_t num_qwords);
Result GetConfig(u64 *out, size_t num_qwords, spl::ConfigItem which);
Result SetConfig(AsyncOperationKey *out_op, spl::ConfigItem key, const u64 *value, size_t num_qwords, const void *sign);
Result GetConfig(u64 *out, size_t num_qwords, spl::ConfigItem key);
Result GetResult(Result *out, AsyncOperationKey op);
Result GetResultData(Result *out, void *out_buf, size_t out_buf_size, AsyncOperationKey op);
Result ModularExponentiate(AsyncOperationKey *out_op, const void *base, const void *exp, size_t exp_size, const void *mod);
Result GenerateRandomBytes(void *out, size_t size);
Result GenerateAesKek(AccessKey *out, const KeySource &source, u32 generation, u32 option);
Result LoadAesKey(u32 keyslot, const AccessKey &access_key, const KeySource &source);
Result ComputeAes(AsyncOperationKey *out_op, u32 mode, const IvCtr &iv_ctr, u32 dst_addr, u32 src_addr, size_t size);
Result ComputeAes(AsyncOperationKey *out_op, u32 dst_addr, u32 mode, const IvCtr &iv_ctr, u32 src_addr, size_t size);
Result GenerateSpecificAesKey(AesKey *out_key, const KeySource &source, u32 generation, u32 which);
Result ComputeCmac(Cmac *out_mac, u32 keyslot, const void *data, size_t size);
Result ReencryptDeviceUniqueData(void *data, size_t size, const AccessKey &access_key_dec, const KeySource &source_dec, const AccessKey &access_key_enc, const KeySource &source_enc, u32 option);
@ -59,12 +59,13 @@ namespace ams::spl::smc {
Result AtmosphereGetEmummcConfig(void *out_config, void *out_paths, u32 storage_id);
/* Helpers. */
inline Result SetConfig(spl::ConfigItem which, const u64 *value, size_t num_qwords) {
return SetConfig(which, nullptr, value, num_qwords);
ALWAYS_INLINE Result SetConfig(spl::ConfigItem key, const u64 *value, size_t num_qwords) {
AsyncOperationKey dummy_op;
return SetConfig(std::addressof(dummy_op), key, value, num_qwords, nullptr);
}
inline Result SetConfig(spl::ConfigItem which, const u64 value) {
return SetConfig(which, std::addressof(value), 1);
ALWAYS_INLINE Result SetConfig(spl::ConfigItem key, const u64 value) {
return SetConfig(key, std::addressof(value), 1);
}
}

View file

@ -54,7 +54,7 @@ namespace ams::spl {
enum class Result {
Success = 0,
NotImplemented = 1,
NotSupported = 1,
InvalidArgument = 2,
InProgress = 3,
NoAsyncOperation = 4,
@ -69,7 +69,7 @@ namespace ams::spl {
/* Convert to the list of known SecureMonitorErrors. */
const auto converted = R_MAKE_NAMESPACE_RESULT(::ams::spl, static_cast<u32>(smc_result));
R_UNLESS(spl::ResultSecureMonitorError::Includes(converted), spl::ResultUnknownSecureMonitorError());
R_UNLESS(spl::ResultSecureMonitorError::Includes(converted), spl::ResultUnexpectedSecureMonitorResult());
/* Return the error. */
return converted;
@ -95,7 +95,7 @@ namespace ams::spl {
DrmDeviceCert = 2,
};
enum class EsCommonKeyType {
enum class EsDeviceUniqueKeyType {
TitleKey = 0,
ArchiveKey = 1,
};
@ -105,6 +105,9 @@ namespace ams::spl {
};
}
constexpr inline size_t AesKeySize = crypto::AesEncryptor128::KeySize;
constexpr inline size_t AesBlockSize = crypto::AesEncryptor128::BlockSize;
enum class HardwareType {
Icosa = 0,
Copper = 1,
@ -168,40 +171,40 @@ namespace ams::spl {
struct AesKey {
union {
u8 data[AES_128_KEY_SIZE];
u64 data64[AES_128_KEY_SIZE / sizeof(u64)];
u8 data[AesKeySize];
u64 data64[AesKeySize / sizeof(u64)];
};
};
static_assert(alignof(AesKey) == alignof(u8), "AesKey definition!");
struct IvCtr {
union {
u8 data[AES_128_KEY_SIZE];
u64 data64[AES_128_KEY_SIZE / sizeof(u64)];
u8 data[AesKeySize];
u64 data64[AesKeySize / sizeof(u64)];
};
};
static_assert(alignof(IvCtr) == alignof(u8), "IvCtr definition!");
struct Cmac {
union {
u8 data[AES_128_KEY_SIZE];
u64 data64[AES_128_KEY_SIZE / sizeof(u64)];
u8 data[AesKeySize];
u64 data64[AesKeySize / sizeof(u64)];
};
};
static_assert(alignof(Cmac) == alignof(u8), "Cmac definition!");
struct AccessKey {
union {
u8 data[AES_128_KEY_SIZE];
u64 data64[AES_128_KEY_SIZE / sizeof(u64)];
u8 data[AesKeySize];
u64 data64[AesKeySize / sizeof(u64)];
};
};
static_assert(alignof(AccessKey) == alignof(u8), "AccessKey definition!");
struct KeySource {
union {
u8 data[AES_128_KEY_SIZE];
u64 data64[AES_128_KEY_SIZE / sizeof(u64)];
u8 data[AesKeySize];
u64 data64[AesKeySize / sizeof(u64)];
};
};
static_assert(alignof(AccessKey) == alignof(u8), "KeySource definition!");