erpt: reimplement the sysmodule (#875)

* erpt: reimplement the sysmodule

* fatal: update for latest bindings

* erpt: amend logic for culling orphan attachments
This commit is contained in:
SciresM 2020-04-13 17:07:37 -07:00 committed by GitHub
parent eca5ac01b8
commit 79b9e07ee9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
117 changed files with 6716 additions and 59 deletions

View file

@ -28,3 +28,5 @@
#include <vapours/crypto/crypto_rsa_pss_sha256_verifier.hpp>
#include <vapours/crypto/crypto_rsa_oaep_sha256_decoder.hpp>
#include <vapours/crypto/crypto_rsa_oaep_sha256_decryptor.hpp>
#include <vapours/crypto/crypto_rsa_oaep_sha256_encryptor.hpp>
#include <vapours/crypto/crypto_csrng.hpp>

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2018-2020 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 <vapours/common.hpp>
#include <vapours/assert.hpp>
#include <vapours/util.hpp>
namespace ams::crypto {
void GenerateCryptographicallyRandomBytes(void *dst, size_t dst_size);
}

View file

@ -45,7 +45,7 @@ namespace ams::crypto {
u8 label_digest[HashSize];
State state;
public:
RsaOaepDecryptor() : set_label_digest(false), state(State::None) { /* ... */ }
RsaOaepDecryptor() : set_label_digest(false), state(State::None) { std::memset(this->label_digest, 0, sizeof(this->label_digest)); }
~RsaOaepDecryptor() {
ClearMemory(this->label_digest, sizeof(this->label_digest));
@ -78,21 +78,22 @@ namespace ams::crypto {
size_t Decrypt(void *dst, size_t dst_size, const void *src, size_t src_size) {
AMS_ASSERT(this->state == State::Initialized);
ON_SCOPE_EXIT { this->state = State::Done; };
impl::RsaOaepImpl<Hash> impl;
u8 message[BlockSize];
ON_SCOPE_EXIT { ClearMemory(message, sizeof(message)); };
if (!this->calculator.ExpMod(message, src, src_size)) {
std::memset(dst, 0, dst_size);
return false;
}
if (!this->set_label_digest) {
this->hash.GetHash(this->label_digest, sizeof(this->label_digest));
this->set_label_digest = true;
}
ON_SCOPE_EXIT { this->state = State::Done; };
return impl.Decode(dst, dst_size, this->label_digest, sizeof(this->label_digest), message, sizeof(message));
}

View file

@ -0,0 +1,137 @@
/*
* Copyright (c) 2018-2020 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 <vapours/common.hpp>
#include <vapours/assert.hpp>
#include <vapours/util.hpp>
#include <vapours/crypto/crypto_rsa_calculator.hpp>
#include <vapours/crypto/impl/crypto_rsa_oaep_impl.hpp>
namespace ams::crypto {
template<size_t ModulusSize, typename Hash> /* requires HashFunction<Hash> */
class RsaOaepEncryptor {
NON_COPYABLE(RsaOaepEncryptor);
NON_MOVEABLE(RsaOaepEncryptor);
public:
static constexpr size_t HashSize = Hash::HashSize;
static constexpr size_t BlockSize = ModulusSize;
static constexpr size_t MaximumExponentSize = 3;
static constexpr size_t RequiredWorkBufferSize = RsaCalculator<ModulusSize, MaximumExponentSize>::RequiredWorkBufferSize;
private:
enum class State {
None,
Initialized,
Done,
};
private:
RsaCalculator<ModulusSize, MaximumExponentSize> calculator;
Hash hash;
bool set_label_digest;
u8 label_digest[HashSize];
State state;
public:
RsaOaepEncryptor() : set_label_digest(false), state(State::None) { std::memset(this->label_digest, 0, sizeof(this->label_digest)); }
~RsaOaepEncryptor() {
ClearMemory(this->label_digest, sizeof(this->label_digest));
}
bool Initialize(const void *mod, size_t mod_size, const void *exp, size_t exp_size) {
this->hash.Initialize();
this->set_label_digest = false;
if (this->calculator.Initialize(mod, mod_size, exp, exp_size)) {
this->state = State::Initialized;
return true;
} else {
return false;
}
}
void UpdateLabel(const void *data, size_t size) {
AMS_ASSERT(this->state == State::Initialized);
this->hash.Update(data, size);
}
void SetLabelDigest(const void *digest, size_t digest_size) {
AMS_ASSERT(this->state == State::Initialized);
AMS_ABORT_UNLESS(digest_size == sizeof(this->label_digest));
std::memcpy(this->label_digest, digest, digest_size);
this->set_label_digest = true;
}
bool Encrypt(void *dst, size_t dst_size, const void *src, size_t src_size, const void *salt, size_t salt_size) {
AMS_ASSERT(this->state == State::Initialized);
impl::RsaOaepImpl<Hash> impl;
if (!this->set_label_digest) {
this->hash.GetHash(this->label_digest, sizeof(this->label_digest));
}
impl.Encode(dst, dst_size, this->label_digest, sizeof(this->label_digest), src, src_size, salt, salt_size);
if (!this->calculator.ExpMod(dst, dst, dst_size)) {
std::memset(dst, 0, dst_size);
return false;
}
this->state = State::Done;
return true;
}
bool Encrypt(void *dst, size_t dst_size, const void *src, size_t src_size, const void *salt, size_t salt_size, void *work, size_t work_size) {
AMS_ASSERT(this->state == State::Initialized);
impl::RsaOaepImpl<Hash> impl;
if (!this->set_label_digest) {
this->hash.GetHash(this->label_digest, sizeof(this->label_digest));
}
impl.Encode(dst, dst_size, this->label_digest, sizeof(this->label_digest), src, src_size, salt, salt_size);
if (!this->calculator.ExpMod(dst, dst, dst_size, work, work_size)) {
std::memset(dst, 0, dst_size);
return false;
}
this->state = State::Done;
return true;
}
static bool Encrypt(void *dst, size_t dst_size, const void *mod, size_t mod_size, const void *exp, size_t exp_size, const void *msg, size_t msg_size, const void *seed, size_t seed_size, const void *lab, size_t lab_size) {
RsaOaepEncryptor<ModulusSize, Hash> oaep;
if (!oaep.Initialize(mod, mod_size, exp, exp_size)) {
return false;
}
oaep.UpdateLabel(lab, lab_size);
return oaep.Encrypt(dst, dst_size, msg, msg_size, seed, seed_size);
}
static bool Encrypt(void *dst, size_t dst_size, const void *mod, size_t mod_size, const void *exp, size_t exp_size, const void *msg, size_t msg_size, const void *seed, size_t seed_size, const void *lab, size_t lab_size, void *work, size_t work_size) {
RsaOaepEncryptor<ModulusSize, Hash> oaep;
if (!oaep.Initialize(mod, mod_size, exp, exp_size)) {
return false;
}
oaep.UpdateLabel(lab, lab_size);
return oaep.Encrypt(dst, dst_size, msg, msg_size, seed, seed_size, work, work_size);
}
};
}

View file

@ -0,0 +1,53 @@
/*
* Copyright (c) 2018-2020 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 <vapours/common.hpp>
#include <vapours/assert.hpp>
#include <vapours/util.hpp>
#include <vapours/crypto/crypto_rsa_calculator.hpp>
#include <vapours/crypto/crypto_rsa_oaep_encryptor.hpp>
#include <vapours/crypto/crypto_sha256_generator.hpp>
namespace ams::crypto {
namespace impl {
template<size_t Bits>
using RsaNOaepSha256Encryptor = ::ams::crypto::RsaOaepEncryptor<Bits / BITSIZEOF(u8), ::ams::crypto::Sha256Generator>;
}
using Rsa2048OaepSha256Encryptor = ::ams::crypto::impl::RsaNOaepSha256Encryptor<2048>;
using Rsa4096OaepSha256Encryptor = ::ams::crypto::impl::RsaNOaepSha256Encryptor<4096>;
inline size_t EncryptRsa2048OaepSha256(void *dst, size_t dst_size, const void *mod, size_t mod_size, const void *exp, size_t exp_size, const void *msg, size_t msg_size, const void *salt, size_t salt_size, const void *lab, size_t lab_size) {
return Rsa2048OaepSha256Encryptor::Encrypt(dst, dst_size, mod, mod_size, exp, exp_size, msg, msg_size, salt, salt_size, lab, lab_size);
}
inline size_t EncryptRsa2048OaepSha256(void *dst, size_t dst_size, const void *mod, size_t mod_size, const void *exp, size_t exp_size, const void *msg, size_t msg_size, const void *salt, size_t salt_size, const void *lab, size_t lab_size, void *work_buf, size_t work_buf_size) {
return Rsa2048OaepSha256Encryptor::Encrypt(dst, dst_size, mod, mod_size, exp, exp_size, msg, msg_size, salt, salt_size, lab, lab_size, work_buf, work_buf_size);
}
inline size_t EncryptRsa4096OaepSha256(void *dst, size_t dst_size, const void *mod, size_t mod_size, const void *exp, size_t exp_size, const void *msg, size_t msg_size, const void *salt, size_t salt_size, const void *lab, size_t lab_size) {
return Rsa4096OaepSha256Encryptor::Encrypt(dst, dst_size, mod, mod_size, exp, exp_size, msg, msg_size, salt, salt_size, lab, lab_size);
}
inline size_t EncryptRsa4096OaepSha256(void *dst, size_t dst_size, const void *mod, size_t mod_size, const void *exp, size_t exp_size, const void *msg, size_t msg_size, const void *salt, size_t salt_size, const void *lab, size_t lab_size, void *work_buf, size_t work_buf_size) {
return Rsa4096OaepSha256Encryptor::Encrypt(dst, dst_size, mod, mod_size, exp, exp_size, msg, msg_size, salt, salt_size, lab, lab_size, work_buf, work_buf_size);
}
}

View file

@ -71,6 +71,39 @@ namespace ams::crypto::impl {
public:
RsaOaepImpl() { /* ... */ }
void Encode(void *dst, size_t dst_size, Hash *hash, const void *src, size_t src_size, const void *salt, size_t salt_size) {
u8 label_digest[HashSize];
ON_SCOPE_EXIT { ClearMemory(label_digest, HashSize); };
hash->GetHash(label_digest, HashSize);
return this->Encode(dst, dst_size, label_digest, sizeof(label_digest), src, src_size, salt, salt_size);
}
void Encode(void *dst, size_t dst_size, const void *label_digest, size_t label_digest_size, const void *src, size_t src_size, const void *salt, size_t salt_size) {
/* Check our preconditions. */
AMS_ASSERT(dst_size >= 2 * HashSize + 2 + src_size);
AMS_ASSERT(salt_size > 0);
AMS_ASSERT(salt_size == HashSize);
AMS_ASSERT(label_digest_size == HashSize);
u8 *buf = static_cast<u8 *>(dst);
buf[0] = HeadMagic;
u8 *seed = buf + 1;
std::memcpy(seed, salt, HashSize);
u8 *db = seed + HashSize;
std::memcpy(db, label_digest, HashSize);
std::memset(db + HashSize, 0, dst_size - 2 * HashSize - 2 - src_size);
u8 *msg = buf + dst_size - src_size - 1;
*(msg++) = 0x01;
std::memcpy(msg, src, src_size);
ApplyMGF1(db, dst_size - (1 + HashSize), seed, HashSize);
ApplyMGF1(seed, HashSize, db, dst_size - (1 + HashSize));
}
size_t Decode(void *dst, size_t dst_size, const void *label_digest, size_t label_digest_size, u8 *buf, size_t buf_size) {
/* Check our preconditions. */
AMS_ABORT_UNLESS(dst_size > 0);

View file

@ -27,6 +27,7 @@
#include <vapours/results/creport_results.hpp>
#include <vapours/results/debug_results.hpp>
#include <vapours/results/dmnt_results.hpp>
#include <vapours/results/erpt_results.hpp>
#include <vapours/results/err_results.hpp>
#include <vapours/results/fatal_results.hpp>
#include <vapours/results/fs_results.hpp>
@ -38,12 +39,14 @@
#include <vapours/results/os_results.hpp>
#include <vapours/results/ncm_results.hpp>
#include <vapours/results/pm_results.hpp>
#include <vapours/results/psc_results.hpp>
#include <vapours/results/ro_results.hpp>
#include <vapours/results/settings_results.hpp>
#include <vapours/results/sf_results.hpp>
#include <vapours/results/sm_results.hpp>
#include <vapours/results/spl_results.hpp>
#include <vapours/results/svc_results.hpp>
#include <vapours/results/time_results.hpp>
#include <vapours/results/updater_results.hpp>
#include <vapours/results/vi_results.hpp>

View file

@ -0,0 +1,43 @@
/*
* Copyright (c) 2018-2020 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 <vapours/results/results_common.hpp>
namespace ams::erpt {
R_DEFINE_NAMESPACE_RESULT_MODULE(147);
R_DEFINE_ERROR_RESULT(NotInitialized, 1);
R_DEFINE_ERROR_RESULT(AlreadyInitialized, 2);
R_DEFINE_ERROR_RESULT(OutOfArraySpace, 3);
R_DEFINE_ERROR_RESULT(OutOfFieldSpace, 4);
R_DEFINE_ERROR_RESULT(OutOfMemory, 5);
R_DEFINE_ERROR_RESULT(InvalidArgument, 7);
R_DEFINE_ERROR_RESULT(NotFound, 8);
R_DEFINE_ERROR_RESULT(FieldCategoryMismatch, 9);
R_DEFINE_ERROR_RESULT(FieldTypeMismatch, 10);
R_DEFINE_ERROR_RESULT(AlreadyExists, 11);
R_DEFINE_ERROR_RESULT(CorruptJournal, 12);
R_DEFINE_ERROR_RESULT(CategoryNotFound, 13);
R_DEFINE_ERROR_RESULT(RequiredContextMissing, 14);
R_DEFINE_ERROR_RESULT(RequiredFieldMissing, 15);
R_DEFINE_ERROR_RESULT(FormatterError, 16);
R_DEFINE_ERROR_RESULT(InvalidPowerState, 17);
R_DEFINE_ERROR_RESULT(ArrayFieldTooLarge, 18);
R_DEFINE_ERROR_RESULT(AlreadyOwned, 19);
}

View file

@ -0,0 +1,27 @@
/*
* Copyright (c) 2018-2020 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 <vapours/results/results_common.hpp>
namespace ams::psc {
R_DEFINE_NAMESPACE_RESULT_MODULE(138);
R_DEFINE_ERROR_RESULT(AlreadyInitialized, 2);
R_DEFINE_ERROR_RESULT(NotInitialized, 3);
}

View file

@ -0,0 +1,32 @@
/*
* Copyright (c) 2018-2020 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 <vapours/results/results_common.hpp>
namespace ams::time {
R_DEFINE_NAMESPACE_RESULT_MODULE(116);
R_DEFINE_ERROR_RESULT(NotInitialized, 0);
R_DEFINE_ERROR_RESULT(NotComparable, 200);
R_DEFINE_ERROR_RESULT(Overflowed, 201);
R_DEFINE_ABSTRACT_ERROR_RANGE(InvalidArgument, 900, 919);
R_DEFINE_ERROR_RESULT(InvalidPointer, 901);
}

View file

@ -34,3 +34,4 @@
#include <vapours/util/util_tinymt.hpp>
#include <vapours/util/util_uuid.hpp>
#include <vapours/util/util_bounded_map.hpp>
#include <vapours/util/util_string_util.hpp>

View file

@ -178,9 +178,9 @@ namespace ams::util {
constexpr BitFlagSet<N, T> operator^(const BitFlagSet<N, T> &rhs) const { BitFlagSet<N, T> v = *this; v ^= rhs; return v; }
constexpr BitFlagSet<N, T> operator|(const BitFlagSet<N, T> &rhs) const { BitFlagSet<N, T> v = *this; v |= rhs; return v; }
constexpr BitFlagSet<N, T> &operator&=(const BitFlagSet<N, T> &rhs) const { ams::util::impl::AndImpl<StorageCount>(this->_storage, rhs._storage); return *this; }
constexpr BitFlagSet<N, T> &operator^=(const BitFlagSet<N, T> &rhs) const { ams::util::impl::XorImpl<StorageCount>(this->_storage, rhs._storage); return *this; }
constexpr BitFlagSet<N, T> &operator|=(const BitFlagSet<N, T> &rhs) const { ams::util::impl::OrImpl<StorageCount>(this->_storage, rhs._storage); return *this; }
constexpr BitFlagSet<N, T> &operator&=(const BitFlagSet<N, T> &rhs) { ams::util::impl::AndImpl<StorageCount>(this->_storage, rhs._storage); return *this; }
constexpr BitFlagSet<N, T> &operator^=(const BitFlagSet<N, T> &rhs) { ams::util::impl::XorImpl<StorageCount>(this->_storage, rhs._storage); return *this; }
constexpr BitFlagSet<N, T> &operator|=(const BitFlagSet<N, T> &rhs) { ams::util::impl::OrImpl<StorageCount>(this->_storage, rhs._storage); return *this; }
};
template<size_t N, typename T>

View file

@ -0,0 +1,43 @@
/*
* Copyright (c) 2018-2020 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 <vapours/common.hpp>
#include <vapours/assert.hpp>
namespace ams::util {
template<typename T>
constexpr int Strlcpy(T *dst, const T *src, int count) {
AMS_ASSERT(dst != nullptr);
AMS_ASSERT(src != nullptr);
const T *cur = src;
if (count > 0) {
while ((--count) && *cur) {
*(dst++) = *(cur++);
}
*dst = 0;
}
while (*cur) {
cur++;
}
return static_cast<int>(cur - src);
}
}

View file

@ -21,20 +21,60 @@
namespace ams::util {
struct Uuid {
static constexpr size_t Size = 0x10;
static constexpr size_t Size = 0x10;
static constexpr size_t StringSize = sizeof("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
u8 data[Size];
bool operator==(const Uuid &rhs) const {
return std::memcmp(this->data, rhs.data, Size) == 0;
friend bool operator==(const Uuid &lhs, const Uuid &rhs) {
return std::memcmp(lhs.data, rhs.data, Size) == 0;
}
bool operator!=(const Uuid &rhs) const {
return !(*this == rhs);
friend bool operator!=(const Uuid &lhs, const Uuid &rhs) {
return !(lhs == rhs);
}
u8 operator[](size_t i) const {
return this->data[i];
const char *ToString(char *dst, size_t dst_size) const {
std::snprintf(dst, dst_size, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
this->data[ 0], this->data[ 1], this->data[ 2], this->data[ 3], this->data[ 4], this->data[ 5], this->data[ 6], this->data[ 7],
this->data[ 8], this->data[ 9], this->data[10], this->data[11], this->data[12], this->data[13], this->data[14], this->data[15]);
return dst;
}
void FromString(const char *str) {
char buf[2 + 1] = {};
char *end;
s32 i = 0;
for (/* ... */; i < 4; ++i, str += 2) {
std::memcpy(buf, str, 2);
this->data[i] = static_cast<u8>(std::strtoul(buf, std::addressof(end), 16));
}
++str;
for (/* ... */; i < 6; ++i, str += 2) {
std::memcpy(buf, str, 2);
this->data[i] = static_cast<u8>(std::strtoul(buf, std::addressof(end), 16));
}
++str;
for (/* ... */; i < 8; ++i, str += 2) {
std::memcpy(buf, str, 2);
this->data[i] = static_cast<u8>(std::strtoul(buf, std::addressof(end), 16));
}
++str;
for (/* ... */; i < 10; ++i, str += 2) {
std::memcpy(buf, str, 2);
this->data[i] = static_cast<u8>(std::strtoul(buf, std::addressof(end), 16));
}
++str;
for (/* ... */; i < 16; ++i, str += 2) {
std::memcpy(buf, str, 2);
this->data[i] = static_cast<u8>(std::strtoul(buf, std::addressof(end), 16));
}
}
};