mirror of
https://github.com/Atmosphere-NX/Atmosphere
synced 2024-12-22 12:21:18 +00:00
exo/vapours: refactor member variables to m_ over this->
This commit is contained in:
parent
5a38311ebf
commit
67a45c97ef
55 changed files with 846 additions and 847 deletions
|
@ -22,14 +22,14 @@ namespace ams::secmon::loader {
|
||||||
|
|
||||||
class Lz4Uncompressor {
|
class Lz4Uncompressor {
|
||||||
private:
|
private:
|
||||||
const u8 *src;
|
const u8 *m_src;
|
||||||
size_t src_size;
|
size_t m_src_size;
|
||||||
size_t src_offset;
|
size_t m_src_offset;
|
||||||
u8 *dst;
|
u8 *m_dst;
|
||||||
size_t dst_size;
|
size_t m_dst_size;
|
||||||
size_t dst_offset;
|
size_t m_dst_offset;
|
||||||
public:
|
public:
|
||||||
Lz4Uncompressor(void *dst, size_t dst_size, const void *src, size_t src_size) : src(static_cast<const u8 *>(src)), src_size(src_size), src_offset(0), dst(static_cast<u8 *>(dst)), dst_size(dst_size), dst_offset(0) {
|
Lz4Uncompressor(void *dst, size_t dst_size, const void *src, size_t src_size) : m_src(static_cast<const u8 *>(src)), m_src_size(src_size), m_src_offset(0), m_dst(static_cast<u8 *>(dst)), m_dst_size(dst_size), m_dst_offset(0) {
|
||||||
/* ... */
|
/* ... */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ namespace ams::secmon::loader {
|
||||||
this->Copy(this->GetCopySize(control >> 4));
|
this->Copy(this->GetCopySize(control >> 4));
|
||||||
|
|
||||||
/* If we've exceeded size, we're done. */
|
/* If we've exceeded size, we're done. */
|
||||||
if (this->src_offset >= this->src_size) {
|
if (m_src_offset >= m_src_size) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,21 +55,21 @@ namespace ams::secmon::loader {
|
||||||
const size_t wide_copy_size = this->GetCopySize(control & 0xF);
|
const size_t wide_copy_size = this->GetCopySize(control & 0xF);
|
||||||
|
|
||||||
/* Copy bytes. */
|
/* Copy bytes. */
|
||||||
const size_t end_offset = this->dst_offset + wide_copy_size + 4;
|
const size_t end_offset = m_dst_offset + wide_copy_size + 4;
|
||||||
for (size_t cur_offset = this->dst_offset; cur_offset < end_offset; this->dst_offset = (++cur_offset)) {
|
for (size_t cur_offset = m_dst_offset; cur_offset < end_offset; m_dst_offset = (++cur_offset)) {
|
||||||
AMS_ABORT_UNLESS(wide_offset <= cur_offset);
|
AMS_ABORT_UNLESS(wide_offset <= cur_offset);
|
||||||
|
|
||||||
this->dst[cur_offset] = this->dst[cur_offset - wide_offset];
|
m_dst[cur_offset] = m_dst[cur_offset - wide_offset];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
u8 ReadByte() {
|
u8 ReadByte() {
|
||||||
return this->src[this->src_offset++];
|
return m_src[m_src_offset++];
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CanRead() const {
|
bool CanRead() const {
|
||||||
return this->src_offset < this->src_size;
|
return m_src_offset < m_src_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t GetCopySize(u8 control) {
|
size_t GetCopySize(u8 control) {
|
||||||
|
@ -87,9 +87,9 @@ namespace ams::secmon::loader {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Copy(size_t size) {
|
void Copy(size_t size) {
|
||||||
__builtin_memcpy(this->dst + this->dst_offset, this->src + this->src_offset, size);
|
__builtin_memcpy(m_dst + m_dst_offset, m_src + m_src_offset, size);
|
||||||
this->dst_offset += size;
|
m_dst_offset += size;
|
||||||
this->src_offset += size;
|
m_src_offset += size;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -76,10 +76,10 @@ namespace ams::secmon::fatal {
|
||||||
Bit_Readable = 31,
|
Bit_Readable = 31,
|
||||||
};
|
};
|
||||||
private:
|
private:
|
||||||
u32 value;
|
u32 m_value;
|
||||||
protected:
|
protected:
|
||||||
constexpr ALWAYS_INLINE u32 SelectBit(Bit n) const {
|
constexpr ALWAYS_INLINE u32 SelectBit(Bit n) const {
|
||||||
return (this->value & (1u << n));
|
return (m_value & (1u << n));
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE bool GetBit(Bit n) const {
|
constexpr ALWAYS_INLINE bool GetBit(Bit n) const {
|
||||||
|
@ -97,7 +97,7 @@ namespace ams::secmon::fatal {
|
||||||
ALWAYS_INLINE void SetValue(u32 v) {
|
ALWAYS_INLINE void SetValue(u32 v) {
|
||||||
/* Prevent re-ordering around entry modifications. */
|
/* Prevent re-ordering around entry modifications. */
|
||||||
__asm__ __volatile__("" ::: "memory");
|
__asm__ __volatile__("" ::: "memory");
|
||||||
this->value = v;
|
m_value = v;
|
||||||
__asm__ __volatile__("" ::: "memory");
|
__asm__ __volatile__("" ::: "memory");
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
|
@ -112,7 +112,7 @@ namespace ams::secmon::fatal {
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE u32 GetAttributes() const { return this->SelectBit(Bit_NonSecure) | this->SelectBit(Bit_Writeable) | this->SelectBit(Bit_Readable); }
|
constexpr ALWAYS_INLINE u32 GetAttributes() const { return this->SelectBit(Bit_NonSecure) | this->SelectBit(Bit_Writeable) | this->SelectBit(Bit_Readable); }
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE dd::PhysicalAddress GetPhysicalAddress() const { return (static_cast<u64>(this->value) << DevicePageBits) & PhysicalAddressMask; }
|
constexpr ALWAYS_INLINE dd::PhysicalAddress GetPhysicalAddress() const { return (static_cast<u64>(m_value) << DevicePageBits) & PhysicalAddressMask; }
|
||||||
|
|
||||||
ALWAYS_INLINE void Invalidate() { this->SetValue(0); }
|
ALWAYS_INLINE void Invalidate() { this->SetValue(0); }
|
||||||
};
|
};
|
||||||
|
|
|
@ -28,7 +28,7 @@ namespace ams::fs {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ReadOption {
|
struct ReadOption {
|
||||||
u32 value;
|
u32 _value;
|
||||||
|
|
||||||
static const ReadOption None;
|
static const ReadOption None;
|
||||||
};
|
};
|
||||||
|
@ -36,7 +36,7 @@ namespace ams::fs {
|
||||||
inline constexpr const ReadOption ReadOption::None = {0};
|
inline constexpr const ReadOption ReadOption::None = {0};
|
||||||
|
|
||||||
inline constexpr bool operator==(const ReadOption &lhs, const ReadOption &rhs) {
|
inline constexpr bool operator==(const ReadOption &lhs, const ReadOption &rhs) {
|
||||||
return lhs.value == rhs.value;
|
return lhs._value == rhs._value;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline constexpr bool operator!=(const ReadOption &lhs, const ReadOption &rhs) {
|
inline constexpr bool operator!=(const ReadOption &lhs, const ReadOption &rhs) {
|
||||||
|
@ -46,10 +46,10 @@ namespace ams::fs {
|
||||||
static_assert(util::is_pod<ReadOption>::value && sizeof(ReadOption) == sizeof(u32));
|
static_assert(util::is_pod<ReadOption>::value && sizeof(ReadOption) == sizeof(u32));
|
||||||
|
|
||||||
struct WriteOption {
|
struct WriteOption {
|
||||||
u32 value;
|
u32 _value;
|
||||||
|
|
||||||
constexpr inline bool HasFlushFlag() const {
|
constexpr inline bool HasFlushFlag() const {
|
||||||
return this->value & 1;
|
return _value & 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const WriteOption None;
|
static const WriteOption None;
|
||||||
|
@ -60,7 +60,7 @@ namespace ams::fs {
|
||||||
inline constexpr const WriteOption WriteOption::Flush = {1};
|
inline constexpr const WriteOption WriteOption::Flush = {1};
|
||||||
|
|
||||||
inline constexpr bool operator==(const WriteOption &lhs, const WriteOption &rhs) {
|
inline constexpr bool operator==(const WriteOption &lhs, const WriteOption &rhs) {
|
||||||
return lhs.value == rhs.value;
|
return lhs._value == rhs._value;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline constexpr bool operator!=(const WriteOption &lhs, const WriteOption &rhs) {
|
inline constexpr bool operator!=(const WriteOption &lhs, const WriteOption &rhs) {
|
||||||
|
|
|
@ -23,16 +23,16 @@ namespace ams::secmon {
|
||||||
|
|
||||||
void *PageMapperImpl::GetPointerTo(uintptr_t phys, size_t size) const {
|
void *PageMapperImpl::GetPointerTo(uintptr_t phys, size_t size) const {
|
||||||
/* Ensure we stay within the page. */
|
/* Ensure we stay within the page. */
|
||||||
if (util::AlignDown(phys, 4_KB) != this->physical_address) {
|
if (util::AlignDown(phys, 4_KB) != m_physical_address) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
if (size != 0) {
|
if (size != 0) {
|
||||||
if (util::AlignDown(phys + size - 1, 4_KB) != this->physical_address) {
|
if (util::AlignDown(phys + size - 1, 4_KB) != m_physical_address) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return reinterpret_cast<void *>(phys + (this->virtual_address - this->physical_address));
|
return reinterpret_cast<void *>(phys + (m_virtual_address - m_physical_address));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PageMapperImpl::CopyToMapping(uintptr_t dst_phys, const void *src, size_t size) const {
|
bool PageMapperImpl::CopyToMapping(uintptr_t dst_phys, const void *src, size_t size) const {
|
||||||
|
|
|
@ -22,10 +22,10 @@ namespace ams::secmon {
|
||||||
|
|
||||||
class PageMapperImpl {
|
class PageMapperImpl {
|
||||||
private:
|
private:
|
||||||
uintptr_t physical_address;
|
uintptr_t m_physical_address;
|
||||||
uintptr_t virtual_address;
|
uintptr_t m_virtual_address;
|
||||||
public:
|
public:
|
||||||
constexpr PageMapperImpl(uintptr_t phys) : physical_address(util::AlignDown(phys, 4_KB)), virtual_address() { /* ... */ }
|
constexpr PageMapperImpl(uintptr_t phys) : m_physical_address(util::AlignDown(phys, 4_KB)), m_virtual_address() { /* ... */ }
|
||||||
|
|
||||||
void *GetPointerTo(uintptr_t phys, size_t size) const;
|
void *GetPointerTo(uintptr_t phys, size_t size) const;
|
||||||
|
|
||||||
|
@ -37,14 +37,14 @@ namespace ams::secmon {
|
||||||
|
|
||||||
template<auto F>
|
template<auto F>
|
||||||
bool MapImpl() {
|
bool MapImpl() {
|
||||||
this->virtual_address = F(this->physical_address);
|
m_virtual_address = F(m_physical_address);
|
||||||
return this->virtual_address != 0;
|
return m_virtual_address != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<auto F>
|
template<auto F>
|
||||||
void UnmapImpl() {
|
void UnmapImpl() {
|
||||||
F();
|
F();
|
||||||
this->virtual_address = 0;
|
m_virtual_address = 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -55,31 +55,31 @@ namespace ams::secmon::smc {
|
||||||
|
|
||||||
class PrepareEsDeviceUniqueKeyAsyncArguments {
|
class PrepareEsDeviceUniqueKeyAsyncArguments {
|
||||||
private:
|
private:
|
||||||
int generation;
|
int m_generation;
|
||||||
EsCommonKeyType type;
|
EsCommonKeyType m_type;
|
||||||
u8 label_digest[crypto::Sha256Generator::HashSize];
|
u8 m_label_digest[crypto::Sha256Generator::HashSize];
|
||||||
public:
|
public:
|
||||||
void Set(int gen, EsCommonKeyType t, const u8 ld[crypto::Sha256Generator::HashSize]) {
|
void Set(int gen, EsCommonKeyType t, const u8 ld[crypto::Sha256Generator::HashSize]) {
|
||||||
this->generation = gen;
|
m_generation = gen;
|
||||||
this->type = t;
|
m_type = t;
|
||||||
std::memcpy(this->label_digest, ld, sizeof(this->label_digest));
|
std::memcpy(m_label_digest, ld, sizeof(m_label_digest));
|
||||||
}
|
}
|
||||||
|
|
||||||
int GetKeyGeneration() const { return this->generation; }
|
int GetKeyGeneration() const { return m_generation; }
|
||||||
EsCommonKeyType GetCommonKeyType() const { return this->type; }
|
EsCommonKeyType GetCommonKeyType() const { return m_type; }
|
||||||
void GetLabelDigest(u8 dst[crypto::Sha256Generator::HashSize]) const { std::memcpy(dst, this->label_digest, sizeof(this->label_digest)); }
|
void GetLabelDigest(u8 dst[crypto::Sha256Generator::HashSize]) const { std::memcpy(dst, m_label_digest, sizeof(m_label_digest)); }
|
||||||
};
|
};
|
||||||
|
|
||||||
class ModularExponentiateByStorageKeyAsyncArguments {
|
class ModularExponentiateByStorageKeyAsyncArguments {
|
||||||
private:
|
private:
|
||||||
u8 msg[se::RsaSize];
|
u8 m_msg[se::RsaSize];
|
||||||
public:
|
public:
|
||||||
void Set(const void *m, size_t m_size) {
|
void Set(const void *m, size_t m_size) {
|
||||||
AMS_UNUSED(m_size);
|
AMS_UNUSED(m_size);
|
||||||
std::memcpy(this->msg, m, sizeof(this->msg));
|
std::memcpy(m_msg, m, sizeof(m_msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
const u8 *GetMessage() const { return this->msg; }
|
const u8 *GetMessage() const { return m_msg; }
|
||||||
};
|
};
|
||||||
|
|
||||||
constinit SmcResult g_exp_mod_result = SmcResult::Success;
|
constinit SmcResult g_exp_mod_result = SmcResult::Success;
|
||||||
|
|
|
@ -22,14 +22,14 @@ namespace ams::nxboot::loader {
|
||||||
|
|
||||||
class Lz4Uncompressor {
|
class Lz4Uncompressor {
|
||||||
private:
|
private:
|
||||||
const u8 *src;
|
const u8 *m_src;
|
||||||
size_t src_size;
|
size_t m_src_size;
|
||||||
size_t src_offset;
|
size_t m_src_offset;
|
||||||
u8 *dst;
|
u8 *m_dst;
|
||||||
size_t dst_size;
|
size_t m_dst_size;
|
||||||
size_t dst_offset;
|
size_t m_dst_offset;
|
||||||
public:
|
public:
|
||||||
Lz4Uncompressor(void *dst, size_t dst_size, const void *src, size_t src_size) : src(static_cast<const u8 *>(src)), src_size(src_size), src_offset(0), dst(static_cast<u8 *>(dst)), dst_size(dst_size), dst_offset(0) {
|
Lz4Uncompressor(void *dst, size_t dst_size, const void *src, size_t src_size) : m_src(static_cast<const u8 *>(src)), m_src_size(src_size), m_src_offset(0), m_dst(static_cast<u8 *>(dst)), m_dst_size(dst_size), m_dst_offset(0) {
|
||||||
/* ... */
|
/* ... */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ namespace ams::nxboot::loader {
|
||||||
this->Copy(this->GetCopySize(control >> 4));
|
this->Copy(this->GetCopySize(control >> 4));
|
||||||
|
|
||||||
/* If we've exceeded size, we're done. */
|
/* If we've exceeded size, we're done. */
|
||||||
if (this->src_offset >= this->src_size) {
|
if (m_src_offset >= m_src_size) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,21 +55,21 @@ namespace ams::nxboot::loader {
|
||||||
const size_t wide_copy_size = this->GetCopySize(control & 0xF);
|
const size_t wide_copy_size = this->GetCopySize(control & 0xF);
|
||||||
|
|
||||||
/* Copy bytes. */
|
/* Copy bytes. */
|
||||||
const size_t end_offset = this->dst_offset + wide_copy_size + 4;
|
const size_t end_offset = m_dst_offset + wide_copy_size + 4;
|
||||||
for (size_t cur_offset = this->dst_offset; cur_offset < end_offset; this->dst_offset = (++cur_offset)) {
|
for (size_t cur_offset = m_dst_offset; cur_offset < end_offset; m_dst_offset = (++cur_offset)) {
|
||||||
AMS_ABORT_UNLESS(wide_offset <= cur_offset);
|
AMS_ABORT_UNLESS(wide_offset <= cur_offset);
|
||||||
|
|
||||||
this->dst[cur_offset] = this->dst[cur_offset - wide_offset];
|
m_dst[cur_offset] = m_dst[cur_offset - wide_offset];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
u8 ReadByte() {
|
u8 ReadByte() {
|
||||||
return this->src[this->src_offset++];
|
return m_src[m_src_offset++];
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CanRead() const {
|
bool CanRead() const {
|
||||||
return this->src_offset < this->src_size;
|
return m_src_offset < m_src_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t GetCopySize(u8 control) {
|
size_t GetCopySize(u8 control) {
|
||||||
|
@ -87,11 +87,9 @@ namespace ams::nxboot::loader {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Copy(size_t size) {
|
void Copy(size_t size) {
|
||||||
for (size_t i = 0; i < size; ++i) {
|
__builtin_memcpy(m_dst + m_dst_offset, m_src + m_src_offset, size);
|
||||||
this->dst[this->dst_offset + i] = this->src[this->src_offset + i];
|
m_dst_offset += size;
|
||||||
}
|
m_src_offset += size;
|
||||||
this->dst_offset += size;
|
|
||||||
this->src_offset += size;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,7 @@ namespace ams::fs {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ReadOption {
|
struct ReadOption {
|
||||||
u32 value;
|
u32 _value;
|
||||||
|
|
||||||
static const ReadOption None;
|
static const ReadOption None;
|
||||||
};
|
};
|
||||||
|
@ -36,7 +36,7 @@ namespace ams::fs {
|
||||||
inline constexpr const ReadOption ReadOption::None = {0};
|
inline constexpr const ReadOption ReadOption::None = {0};
|
||||||
|
|
||||||
inline constexpr bool operator==(const ReadOption &lhs, const ReadOption &rhs) {
|
inline constexpr bool operator==(const ReadOption &lhs, const ReadOption &rhs) {
|
||||||
return lhs.value == rhs.value;
|
return lhs._value == rhs._value;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline constexpr bool operator!=(const ReadOption &lhs, const ReadOption &rhs) {
|
inline constexpr bool operator!=(const ReadOption &lhs, const ReadOption &rhs) {
|
||||||
|
@ -46,10 +46,10 @@ namespace ams::fs {
|
||||||
static_assert(util::is_pod<ReadOption>::value && sizeof(ReadOption) == sizeof(u32));
|
static_assert(util::is_pod<ReadOption>::value && sizeof(ReadOption) == sizeof(u32));
|
||||||
|
|
||||||
struct WriteOption {
|
struct WriteOption {
|
||||||
u32 value;
|
u32 _value;
|
||||||
|
|
||||||
constexpr inline bool HasFlushFlag() const {
|
constexpr inline bool HasFlushFlag() const {
|
||||||
return this->value & 1;
|
return _value & 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const WriteOption None;
|
static const WriteOption None;
|
||||||
|
@ -60,7 +60,7 @@ namespace ams::fs {
|
||||||
inline constexpr const WriteOption WriteOption::Flush = {1};
|
inline constexpr const WriteOption WriteOption::Flush = {1};
|
||||||
|
|
||||||
inline constexpr bool operator==(const WriteOption &lhs, const WriteOption &rhs) {
|
inline constexpr bool operator==(const WriteOption &lhs, const WriteOption &rhs) {
|
||||||
return lhs.value == rhs.value;
|
return lhs._value == rhs._value;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline constexpr bool operator!=(const WriteOption &lhs, const WriteOption &rhs) {
|
inline constexpr bool operator!=(const WriteOption &lhs, const WriteOption &rhs) {
|
||||||
|
|
|
@ -22,14 +22,14 @@ namespace ams::nxboot {
|
||||||
|
|
||||||
class Lz4Uncompressor {
|
class Lz4Uncompressor {
|
||||||
private:
|
private:
|
||||||
const u8 *src;
|
const u8 *m_src;
|
||||||
size_t src_size;
|
size_t m_src_size;
|
||||||
size_t src_offset;
|
size_t m_src_offset;
|
||||||
u8 *dst;
|
u8 *m_dst;
|
||||||
size_t dst_size;
|
size_t m_dst_size;
|
||||||
size_t dst_offset;
|
size_t m_dst_offset;
|
||||||
public:
|
public:
|
||||||
Lz4Uncompressor(void *dst, size_t dst_size, const void *src, size_t src_size) : src(static_cast<const u8 *>(src)), src_size(src_size), src_offset(0), dst(static_cast<u8 *>(dst)), dst_size(dst_size), dst_offset(0) {
|
Lz4Uncompressor(void *dst, size_t dst_size, const void *src, size_t src_size) : m_src(static_cast<const u8 *>(src)), m_src_size(src_size), m_src_offset(0), m_dst(static_cast<u8 *>(dst)), m_dst_size(dst_size), m_dst_offset(0) {
|
||||||
/* ... */
|
/* ... */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ namespace ams::nxboot {
|
||||||
this->Copy(this->GetCopySize(control >> 4));
|
this->Copy(this->GetCopySize(control >> 4));
|
||||||
|
|
||||||
/* If we've exceeded size, we're done. */
|
/* If we've exceeded size, we're done. */
|
||||||
if (this->src_offset >= this->src_size) {
|
if (m_src_offset >= m_src_size) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,21 +55,21 @@ namespace ams::nxboot {
|
||||||
const size_t wide_copy_size = this->GetCopySize(control & 0xF);
|
const size_t wide_copy_size = this->GetCopySize(control & 0xF);
|
||||||
|
|
||||||
/* Copy bytes. */
|
/* Copy bytes. */
|
||||||
const size_t end_offset = this->dst_offset + wide_copy_size + 4;
|
const size_t end_offset = m_dst_offset + wide_copy_size + 4;
|
||||||
for (size_t cur_offset = this->dst_offset; cur_offset < end_offset; this->dst_offset = (++cur_offset)) {
|
for (size_t cur_offset = m_dst_offset; cur_offset < end_offset; m_dst_offset = (++cur_offset)) {
|
||||||
AMS_ABORT_UNLESS(wide_offset <= cur_offset);
|
AMS_ABORT_UNLESS(wide_offset <= cur_offset);
|
||||||
|
|
||||||
this->dst[cur_offset] = this->dst[cur_offset - wide_offset];
|
m_dst[cur_offset] = m_dst[cur_offset - wide_offset];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
u8 ReadByte() {
|
u8 ReadByte() {
|
||||||
return this->src[this->src_offset++];
|
return m_src[m_src_offset++];
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CanRead() const {
|
bool CanRead() const {
|
||||||
return this->src_offset < this->src_size;
|
return m_src_offset < m_src_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t GetCopySize(u8 control) {
|
size_t GetCopySize(u8 control) {
|
||||||
|
@ -87,9 +87,9 @@ namespace ams::nxboot {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Copy(size_t size) {
|
void Copy(size_t size) {
|
||||||
__builtin_memcpy(this->dst + this->dst_offset, this->src + this->src_offset, size);
|
__builtin_memcpy(m_dst + m_dst_offset, m_src + m_src_offset, size);
|
||||||
this->dst_offset += size;
|
m_dst_offset += size;
|
||||||
this->src_offset += size;
|
m_src_offset += size;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -22,52 +22,53 @@ namespace ams::secmon {
|
||||||
using Address = u64;
|
using Address = u64;
|
||||||
|
|
||||||
struct MemoryRegion {
|
struct MemoryRegion {
|
||||||
Address start_address;
|
private:
|
||||||
Address end_address;
|
Address m_start_address;
|
||||||
|
Address m_end_address;
|
||||||
constexpr MemoryRegion(Address address, size_t size) : start_address(address), end_address(address + size) {
|
public:
|
||||||
if (end_address < start_address) {
|
consteval MemoryRegion(Address address, size_t size) : m_start_address(address), m_end_address(address + size) {
|
||||||
__builtin_unreachable();
|
if (m_end_address < m_start_address) {
|
||||||
|
__builtin_unreachable();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
constexpr Address GetStartAddress() const {
|
constexpr Address GetStartAddress() const {
|
||||||
return this->start_address;
|
return m_start_address;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr Address GetAddress() const {
|
constexpr Address GetAddress() const {
|
||||||
return this->GetStartAddress();
|
return this->GetStartAddress();
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr Address GetEndAddress() const {
|
constexpr Address GetEndAddress() const {
|
||||||
return this->end_address;
|
return m_end_address;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr Address GetLastAddress() const {
|
constexpr Address GetLastAddress() const {
|
||||||
return this->end_address - 1;
|
return m_end_address - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr size_t GetSize() const {
|
constexpr size_t GetSize() const {
|
||||||
return this->end_address - this->start_address;
|
return m_end_address - m_start_address;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr bool Contains(Address address, size_t size) const {
|
constexpr bool Contains(Address address, size_t size) const {
|
||||||
return this->start_address <= address && (address + size - 1) <= this->GetLastAddress();
|
return m_start_address <= address && (address + size - 1) <= this->GetLastAddress();
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr bool Contains(const MemoryRegion &rhs) const {
|
constexpr bool Contains(const MemoryRegion &rhs) const {
|
||||||
return this->Contains(rhs.GetStartAddress(), rhs.GetSize());
|
return this->Contains(rhs.GetStartAddress(), rhs.GetSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T = void> requires (std::is_same<T, void>::value || util::is_pod<T>::value)
|
template<typename T = void> requires (std::is_same<T, void>::value || util::is_pod<T>::value)
|
||||||
ALWAYS_INLINE T *GetPointer() const {
|
ALWAYS_INLINE T *GetPointer() const {
|
||||||
return reinterpret_cast<T *>(this->GetAddress());
|
return reinterpret_cast<T *>(this->GetAddress());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T = void> requires (std::is_same<T, void>::value || util::is_pod<T>::value)
|
template<typename T = void> requires (std::is_same<T, void>::value || util::is_pod<T>::value)
|
||||||
ALWAYS_INLINE T *GetEndPointer() const {
|
ALWAYS_INLINE T *GetEndPointer() const {
|
||||||
return reinterpret_cast<T *>(this->GetEndAddress());
|
return reinterpret_cast<T *>(this->GetEndAddress());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
constexpr inline const MemoryRegion MemoryRegionVirtual = MemoryRegion(UINT64_C(0x1F0000000), 2_MB);
|
constexpr inline const MemoryRegion MemoryRegionVirtual = MemoryRegion(UINT64_C(0x1F0000000), 2_MB);
|
||||||
|
|
|
@ -37,7 +37,7 @@ namespace ams::crypto::impl {
|
||||||
AMS_UNUSED(key_size, is_encrypt);
|
AMS_UNUSED(key_size, is_encrypt);
|
||||||
|
|
||||||
/* Set the security engine keyslot. */
|
/* Set the security engine keyslot. */
|
||||||
this->slot = *static_cast<const int *>(key);
|
m_slot = *static_cast<const int *>(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<size_t KeySize>
|
template<size_t KeySize>
|
||||||
|
@ -48,14 +48,14 @@ namespace ams::crypto::impl {
|
||||||
|
|
||||||
if constexpr (KeySize == 16) {
|
if constexpr (KeySize == 16) {
|
||||||
/* Aes 128. */
|
/* Aes 128. */
|
||||||
se::EncryptAes128(dst, dst_size, this->slot, src, src_size);
|
se::EncryptAes128(dst, dst_size, m_slot, src, src_size);
|
||||||
} else if constexpr (KeySize == 24) {
|
} else if constexpr (KeySize == 24) {
|
||||||
/* Aes 192. */
|
/* Aes 192. */
|
||||||
/* TODO: se::EncryptAes192(dst, dst_size, this->slot, src, src_size); */
|
/* TODO: se::EncryptAes192(dst, dst_size, m_slot, src, src_size); */
|
||||||
AMS_UNUSED(dst, dst_size, src, src_size);
|
AMS_UNUSED(dst, dst_size, src, src_size);
|
||||||
} else if constexpr (KeySize == 32) {
|
} else if constexpr (KeySize == 32) {
|
||||||
/* Aes 256. */
|
/* Aes 256. */
|
||||||
/* TODO: se::EncryptAes256(dst, dst_size, this->slot, src, src_size); */
|
/* TODO: se::EncryptAes256(dst, dst_size, m_slot, src, src_size); */
|
||||||
AMS_UNUSED(dst, dst_size, src, src_size);
|
AMS_UNUSED(dst, dst_size, src, src_size);
|
||||||
} else {
|
} else {
|
||||||
/* Invalid key size. */
|
/* Invalid key size. */
|
||||||
|
@ -71,14 +71,14 @@ namespace ams::crypto::impl {
|
||||||
|
|
||||||
if constexpr (KeySize == 16) {
|
if constexpr (KeySize == 16) {
|
||||||
/* Aes 128. */
|
/* Aes 128. */
|
||||||
se::DecryptAes128(dst, dst_size, this->slot, src, src_size);
|
se::DecryptAes128(dst, dst_size, m_slot, src, src_size);
|
||||||
} else if constexpr (KeySize == 24) {
|
} else if constexpr (KeySize == 24) {
|
||||||
/* Aes 192. */
|
/* Aes 192. */
|
||||||
/* TODO: se::DecryptAes192(dst, dst_size, this->slot, src, src_size); */
|
/* TODO: se::DecryptAes192(dst, dst_size, m_slot, src, src_size); */
|
||||||
AMS_UNUSED(dst, dst_size, src, src_size);
|
AMS_UNUSED(dst, dst_size, src, src_size);
|
||||||
} else if constexpr (KeySize == 32) {
|
} else if constexpr (KeySize == 32) {
|
||||||
/* Aes 256. */
|
/* Aes 256. */
|
||||||
/* TODO: se::DecryptAes256(dst, dst_size, this->slot, src, src_size); */
|
/* TODO: se::DecryptAes256(dst, dst_size, m_slot, src, src_size); */
|
||||||
AMS_UNUSED(dst, dst_size, src, src_size);
|
AMS_UNUSED(dst, dst_size, src, src_size);
|
||||||
} else {
|
} else {
|
||||||
/* Invalid key size. */
|
/* Invalid key size. */
|
||||||
|
|
|
@ -38,8 +38,8 @@ namespace ams::crypto {
|
||||||
static constexpr size_t BlockSize = CtrImpl::BlockSize;
|
static constexpr size_t BlockSize = CtrImpl::BlockSize;
|
||||||
static constexpr size_t IvSize = CtrImpl::BlockSize;
|
static constexpr size_t IvSize = CtrImpl::BlockSize;
|
||||||
private:
|
private:
|
||||||
AesImpl aes_impl;
|
AesImpl m_aes_impl;
|
||||||
CtrImpl ctr_impl;
|
CtrImpl m_ctr_impl;
|
||||||
public:
|
public:
|
||||||
AesCtrCryptor() { /* ... */ }
|
AesCtrCryptor() { /* ... */ }
|
||||||
|
|
||||||
|
@ -52,16 +52,16 @@ namespace ams::crypto {
|
||||||
AMS_ASSERT(iv_size == IvSize);
|
AMS_ASSERT(iv_size == IvSize);
|
||||||
AMS_ASSERT(offset >= 0);
|
AMS_ASSERT(offset >= 0);
|
||||||
|
|
||||||
this->aes_impl.Initialize(key, key_size);
|
m_aes_impl.Initialize(key, key_size);
|
||||||
this->ctr_impl.Initialize(std::addressof(this->aes_impl), iv, iv_size, offset);
|
m_ctr_impl.Initialize(std::addressof(m_aes_impl), iv, iv_size, offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SwitchMessage(const void *iv, size_t iv_size) {
|
void SwitchMessage(const void *iv, size_t iv_size) {
|
||||||
return this->ctr_impl.SwitchMessage(iv, iv_size);
|
return m_ctr_impl.SwitchMessage(iv, iv_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Update(void *dst, size_t dst_size, const void *src, size_t src_size) {
|
size_t Update(void *dst, size_t dst_size, const void *src, size_t src_size) {
|
||||||
return this->ctr_impl.Update(dst, dst_size, src, src_size);
|
return m_ctr_impl.Update(dst, dst_size, src, src_size);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -33,20 +33,20 @@ namespace ams::crypto {
|
||||||
static constexpr size_t BlockSize = Impl::BlockSize;
|
static constexpr size_t BlockSize = Impl::BlockSize;
|
||||||
static constexpr size_t RoundKeySize = Impl::RoundKeySize;
|
static constexpr size_t RoundKeySize = Impl::RoundKeySize;
|
||||||
private:
|
private:
|
||||||
Impl impl;
|
Impl m_impl;
|
||||||
public:
|
public:
|
||||||
AesDecryptor() { /* ... */ }
|
AesDecryptor() { /* ... */ }
|
||||||
|
|
||||||
void Initialize(const void *key, size_t key_size) {
|
void Initialize(const void *key, size_t key_size) {
|
||||||
this->impl.Initialize(key, key_size, false);
|
m_impl.Initialize(key, key_size, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DecryptBlock(void *dst, size_t dst_size, const void *src, size_t src_size) const {
|
void DecryptBlock(void *dst, size_t dst_size, const void *src, size_t src_size) const {
|
||||||
return this->impl.DecryptBlock(dst, dst_size, src, src_size);
|
return m_impl.DecryptBlock(dst, dst_size, src, src_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
const u8 *GetRoundKey() const {
|
const u8 *GetRoundKey() const {
|
||||||
return this->impl.GetRoundKey();
|
return m_impl.GetRoundKey();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -33,20 +33,20 @@ namespace ams::crypto {
|
||||||
static constexpr size_t BlockSize = Impl::BlockSize;
|
static constexpr size_t BlockSize = Impl::BlockSize;
|
||||||
static constexpr size_t RoundKeySize = Impl::RoundKeySize;
|
static constexpr size_t RoundKeySize = Impl::RoundKeySize;
|
||||||
private:
|
private:
|
||||||
Impl impl;
|
Impl m_impl;
|
||||||
public:
|
public:
|
||||||
AesEncryptor() { /* ... */ }
|
AesEncryptor() { /* ... */ }
|
||||||
|
|
||||||
void Initialize(const void *key, size_t key_size) {
|
void Initialize(const void *key, size_t key_size) {
|
||||||
this->impl.Initialize(key, key_size, true);
|
m_impl.Initialize(key, key_size, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void EncryptBlock(void *dst, size_t dst_size, const void *src, size_t src_size) const {
|
void EncryptBlock(void *dst, size_t dst_size, const void *src, size_t src_size) const {
|
||||||
return this->impl.EncryptBlock(dst, dst_size, src, src_size);
|
return m_impl.EncryptBlock(dst, dst_size, src, src_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
const u8 *GetRoundKey() const {
|
const u8 *GetRoundKey() const {
|
||||||
return this->impl.GetRoundKey();
|
return m_impl.GetRoundKey();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -37,30 +37,30 @@ namespace ams::crypto {
|
||||||
static constexpr size_t BlockSize = AesImpl::BlockSize;
|
static constexpr size_t BlockSize = AesImpl::BlockSize;
|
||||||
static constexpr size_t MacSize = AesImpl::BlockSize;
|
static constexpr size_t MacSize = AesImpl::BlockSize;
|
||||||
private:
|
private:
|
||||||
AesImpl aes_impl;
|
AesImpl m_aes_impl;
|
||||||
GcmImpl gcm_impl;
|
GcmImpl m_gcm_impl;
|
||||||
public:
|
public:
|
||||||
AesGcmEncryptor() { /* ... */ }
|
AesGcmEncryptor() { /* ... */ }
|
||||||
|
|
||||||
void Initialize(const void *key, size_t key_size, const void *iv, size_t iv_size) {
|
void Initialize(const void *key, size_t key_size, const void *iv, size_t iv_size) {
|
||||||
this->aes_impl.Initialize(key, key_size);
|
m_aes_impl.Initialize(key, key_size);
|
||||||
this->gcm_impl.Initialize(std::addressof(this->aes_impl), iv, iv_size);
|
m_gcm_impl.Initialize(std::addressof(m_aes_impl), iv, iv_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Reset(const void *iv, size_t iv_size) {
|
void Reset(const void *iv, size_t iv_size) {
|
||||||
this->gcm_impl.Reset(iv, iv_size);
|
m_gcm_impl.Reset(iv, iv_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Update(void *dst, size_t dst_size, const void *src, size_t src_size) {
|
size_t Update(void *dst, size_t dst_size, const void *src, size_t src_size) {
|
||||||
return this->gcm_impl.Update(dst, dst_size, src, src_size);
|
return m_gcm_impl.Update(dst, dst_size, src, src_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void UpdateAad(const void *aad, size_t aad_size) {
|
void UpdateAad(const void *aad, size_t aad_size) {
|
||||||
return this->gcm_impl.UpdateAad(aad, aad_size);
|
return m_gcm_impl.UpdateAad(aad, aad_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GetMac(void *dst, size_t dst_size) {
|
void GetMac(void *dst, size_t dst_size) {
|
||||||
return this->gcm_impl.GetMac(dst, dst_size);
|
return m_gcm_impl.GetMac(dst, dst_size);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -42,9 +42,9 @@ namespace ams::crypto {
|
||||||
static_assert(AesImpl1::KeySize == AesImpl2::KeySize);
|
static_assert(AesImpl1::KeySize == AesImpl2::KeySize);
|
||||||
static_assert(AesImpl1::BlockSize == AesImpl2::BlockSize);
|
static_assert(AesImpl1::BlockSize == AesImpl2::BlockSize);
|
||||||
private:
|
private:
|
||||||
AesImpl1 aes_impl_1;
|
AesImpl1 m_aes_impl_1;
|
||||||
AesImpl2 aes_impl_2;
|
AesImpl2 m_aes_impl_2;
|
||||||
XtsImpl xts_impl;
|
XtsImpl m_xts_impl;
|
||||||
public:
|
public:
|
||||||
AesXtsCryptor() { /* ... */ }
|
AesXtsCryptor() { /* ... */ }
|
||||||
|
|
||||||
|
@ -52,17 +52,17 @@ namespace ams::crypto {
|
||||||
AMS_ASSERT(key_size == KeySize);
|
AMS_ASSERT(key_size == KeySize);
|
||||||
AMS_ASSERT(iv_size == IvSize);
|
AMS_ASSERT(iv_size == IvSize);
|
||||||
|
|
||||||
this->aes_impl_1.Initialize(key1, key_size);
|
m_aes_impl_1.Initialize(key1, key_size);
|
||||||
this->aes_impl_2.Initialize(key2, key_size);
|
m_aes_impl_2.Initialize(key2, key_size);
|
||||||
this->xts_impl.Initialize(std::addressof(this->aes_impl_1), std::addressof(this->aes_impl_2), iv, iv_size);
|
m_xts_impl.Initialize(std::addressof(m_aes_impl_1), std::addressof(m_aes_impl_2), iv, iv_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Update(void *dst, size_t dst_size, const void *src, size_t src_size) {
|
size_t Update(void *dst, size_t dst_size, const void *src, size_t src_size) {
|
||||||
return this->xts_impl.Update(dst, dst_size, src, src_size);
|
return m_xts_impl.Update(dst, dst_size, src, src_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Finalize(void *dst, size_t dst_size) {
|
size_t Finalize(void *dst, size_t dst_size) {
|
||||||
return this->xts_impl.Finalize(dst, dst_size);
|
return m_xts_impl.Finalize(dst, dst_size);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -35,24 +35,24 @@ namespace ams::crypto {
|
||||||
static constexpr size_t BlockSize = Impl::BlockSize;
|
static constexpr size_t BlockSize = Impl::BlockSize;
|
||||||
static constexpr size_t IvSize = Impl::IvSize;
|
static constexpr size_t IvSize = Impl::IvSize;
|
||||||
private:
|
private:
|
||||||
Impl impl;
|
Impl m_impl;
|
||||||
public:
|
public:
|
||||||
CtrDecryptor() { /* ... */ }
|
CtrDecryptor() { /* ... */ }
|
||||||
|
|
||||||
void Initialize(const BlockCipher *cipher, const void *iv, size_t iv_size) {
|
void Initialize(const BlockCipher *cipher, const void *iv, size_t iv_size) {
|
||||||
this->impl.Initialize(cipher, iv, iv_size);
|
m_impl.Initialize(cipher, iv, iv_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Initialize(const BlockCipher *cipher, const void *iv, size_t iv_size, s64 offset) {
|
void Initialize(const BlockCipher *cipher, const void *iv, size_t iv_size, s64 offset) {
|
||||||
this->impl.Initialize(cipher, iv, iv_size, offset);
|
m_impl.Initialize(cipher, iv, iv_size, offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SwitchMessage(const void *iv, size_t iv_size) {
|
void SwitchMessage(const void *iv, size_t iv_size) {
|
||||||
this->impl.SwitchMessage(iv, iv_size);
|
m_impl.SwitchMessage(iv, iv_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Update(void *dst, size_t dst_size, const void *src, size_t src_size) {
|
size_t Update(void *dst, size_t dst_size, const void *src, size_t src_size) {
|
||||||
return this->impl.Update(dst, dst_size, src, src_size);
|
return m_impl.Update(dst, dst_size, src, src_size);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -35,24 +35,24 @@ namespace ams::crypto {
|
||||||
static constexpr size_t BlockSize = Impl::BlockSize;
|
static constexpr size_t BlockSize = Impl::BlockSize;
|
||||||
static constexpr size_t IvSize = Impl::IvSize;
|
static constexpr size_t IvSize = Impl::IvSize;
|
||||||
private:
|
private:
|
||||||
Impl impl;
|
Impl m_impl;
|
||||||
public:
|
public:
|
||||||
CtrEncryptor() { /* ... */ }
|
CtrEncryptor() { /* ... */ }
|
||||||
|
|
||||||
void Initialize(const BlockCipher *cipher, const void *iv, size_t iv_size) {
|
void Initialize(const BlockCipher *cipher, const void *iv, size_t iv_size) {
|
||||||
this->impl.Initialize(cipher, iv, iv_size);
|
m_impl.Initialize(cipher, iv, iv_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Initialize(const BlockCipher *cipher, const void *iv, size_t iv_size, s64 offset) {
|
void Initialize(const BlockCipher *cipher, const void *iv, size_t iv_size, s64 offset) {
|
||||||
this->impl.Initialize(cipher, iv, iv_size, offset);
|
m_impl.Initialize(cipher, iv, iv_size, offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SwitchMessage(const void *iv, size_t iv_size) {
|
void SwitchMessage(const void *iv, size_t iv_size) {
|
||||||
this->impl.SwitchMessage(iv, iv_size);
|
m_impl.SwitchMessage(iv, iv_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Update(void *dst, size_t dst_size, const void *src, size_t src_size) {
|
size_t Update(void *dst, size_t dst_size, const void *src, size_t src_size) {
|
||||||
return this->impl.Update(dst, dst_size, src, src_size);
|
return m_impl.Update(dst, dst_size, src, src_size);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -35,29 +35,29 @@ namespace ams::crypto {
|
||||||
static constexpr size_t BlockSize = Impl::BlockSize;
|
static constexpr size_t BlockSize = Impl::BlockSize;
|
||||||
static constexpr size_t MacSize = Impl::MacSize;
|
static constexpr size_t MacSize = Impl::MacSize;
|
||||||
private:
|
private:
|
||||||
Impl impl;
|
Impl m_impl;
|
||||||
public:
|
public:
|
||||||
GcmEncryptor() { /* ... */ }
|
GcmEncryptor() { /* ... */ }
|
||||||
|
|
||||||
void Initialize(const BlockCipher *cipher, const void *iv, size_t iv_size) {
|
void Initialize(const BlockCipher *cipher, const void *iv, size_t iv_size) {
|
||||||
this->impl.Initialize(cipher);
|
m_impl.Initialize(cipher);
|
||||||
this->impl.Reset(iv, iv_size);
|
m_impl.Reset(iv, iv_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Reset(const void *iv, size_t iv_size) {
|
void Reset(const void *iv, size_t iv_size) {
|
||||||
this->impl.Reset(iv, iv_size);
|
m_impl.Reset(iv, iv_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Update(void *dst, size_t dst_size, const void *src, size_t src_size) {
|
size_t Update(void *dst, size_t dst_size, const void *src, size_t src_size) {
|
||||||
return this->impl.Update(dst, dst_size, src, src_size);
|
return m_impl.Update(dst, dst_size, src, src_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void UpdateAad(const void *aad, size_t aad_size) {
|
void UpdateAad(const void *aad, size_t aad_size) {
|
||||||
return this->impl.UpdateAad(aad, aad_size);
|
return m_impl.UpdateAad(aad, aad_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GetMac(void *dst, size_t dst_size) {
|
void GetMac(void *dst, size_t dst_size) {
|
||||||
return this->impl.GetMac(dst, dst_size);
|
return m_impl.GetMac(dst, dst_size);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -32,20 +32,20 @@ namespace ams::crypto {
|
||||||
static constexpr size_t HashSize = Impl::HashSize;
|
static constexpr size_t HashSize = Impl::HashSize;
|
||||||
static constexpr size_t BlockSize = Impl::BlockSize;
|
static constexpr size_t BlockSize = Impl::BlockSize;
|
||||||
private:
|
private:
|
||||||
Impl impl;
|
Impl m_impl;
|
||||||
public:
|
public:
|
||||||
HmacGenerator() { /* ... */ }
|
HmacGenerator() { /* ... */ }
|
||||||
|
|
||||||
void Initialize(const void *key, size_t key_size) {
|
void Initialize(const void *key, size_t key_size) {
|
||||||
return this->impl.Initialize(key, key_size);
|
return m_impl.Initialize(key, key_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Update(const void *data, size_t size) {
|
void Update(const void *data, size_t size) {
|
||||||
return this->impl.Update(data, size);
|
return m_impl.Update(data, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GetMac(void *dst, size_t dst_size) {
|
void GetMac(void *dst, size_t dst_size) {
|
||||||
return this->impl.GetMac(dst, dst_size);
|
return m_impl.GetMac(dst, dst_size);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,17 +29,17 @@ namespace ams::crypto {
|
||||||
public:
|
public:
|
||||||
static constexpr inline size_t RequiredWorkBufferSize = 0x10 * ModulusSize;
|
static constexpr inline size_t RequiredWorkBufferSize = 0x10 * ModulusSize;
|
||||||
private:
|
private:
|
||||||
impl::StaticBigNum<ModulusSize * BITSIZEOF(u8)> modulus;
|
impl::StaticBigNum<ModulusSize * BITSIZEOF(u8)> m_modulus;
|
||||||
impl::StaticBigNum<ExponentSize * BITSIZEOF(u8)> exponent;
|
impl::StaticBigNum<ExponentSize * BITSIZEOF(u8)> m_exponent;
|
||||||
public:
|
public:
|
||||||
RsaCalculator() { /* ... */ }
|
RsaCalculator() { /* ... */ }
|
||||||
~RsaCalculator() { this->exponent.ClearToZero(); }
|
~RsaCalculator() { m_exponent.ClearToZero(); }
|
||||||
|
|
||||||
bool Initialize(const void *mod, size_t mod_size, const void *exp, size_t exp_size) {
|
bool Initialize(const void *mod, size_t mod_size, const void *exp, size_t exp_size) {
|
||||||
if (!this->modulus.Import(mod, mod_size) || this->modulus.IsZero()) {
|
if (!m_modulus.Import(mod, mod_size) || m_modulus.IsZero()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!this->exponent.Import(exp, exp_size) || this->exponent.IsZero()) {
|
if (!m_exponent.Import(exp, exp_size) || m_exponent.IsZero()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -48,7 +48,7 @@ namespace ams::crypto {
|
||||||
bool ExpMod(void *dst, const void *src, size_t size, void *work_buf, size_t work_buf_size) {
|
bool ExpMod(void *dst, const void *src, size_t size, void *work_buf, size_t work_buf_size) {
|
||||||
AMS_ASSERT(work_buf_size >= RequiredWorkBufferSize);
|
AMS_ASSERT(work_buf_size >= RequiredWorkBufferSize);
|
||||||
|
|
||||||
return this->modulus.ExpMod(dst, src, size, this->exponent, static_cast<u32 *>(work_buf), work_buf_size);
|
return m_modulus.ExpMod(dst, src, size, m_exponent, static_cast<u32 *>(work_buf), work_buf_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ExpMod(void *dst, const void *src, size_t size) {
|
bool ExpMod(void *dst, const void *src, size_t size) {
|
||||||
|
|
|
@ -39,23 +39,23 @@ namespace ams::crypto {
|
||||||
Done,
|
Done,
|
||||||
};
|
};
|
||||||
private:
|
private:
|
||||||
RsaCalculator<ModulusSize, MaximumExponentSize> calculator;
|
RsaCalculator<ModulusSize, MaximumExponentSize> m_calculator;
|
||||||
Hash hash;
|
Hash m_hash;
|
||||||
bool set_label_digest;
|
bool m_set_label_digest;
|
||||||
u8 label_digest[HashSize];
|
u8 m_label_digest[HashSize];
|
||||||
State state;
|
State m_state;
|
||||||
public:
|
public:
|
||||||
RsaOaepDecryptor() : set_label_digest(false), state(State::None) { std::memset(this->label_digest, 0, sizeof(this->label_digest)); }
|
RsaOaepDecryptor() : m_set_label_digest(false), m_state(State::None) { std::memset(m_label_digest, 0, sizeof(m_label_digest)); }
|
||||||
|
|
||||||
~RsaOaepDecryptor() {
|
~RsaOaepDecryptor() {
|
||||||
ClearMemory(this->label_digest, sizeof(this->label_digest));
|
ClearMemory(m_label_digest, sizeof(m_label_digest));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Initialize(const void *mod, size_t mod_size, const void *exp, size_t exp_size) {
|
bool Initialize(const void *mod, size_t mod_size, const void *exp, size_t exp_size) {
|
||||||
this->hash.Initialize();
|
m_hash.Initialize();
|
||||||
this->set_label_digest = false;
|
m_set_label_digest = false;
|
||||||
if (this->calculator.Initialize(mod, mod_size, exp, exp_size)) {
|
if (m_calculator.Initialize(mod, mod_size, exp, exp_size)) {
|
||||||
this->state = State::Initialized;
|
m_state = State::Initialized;
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
@ -63,58 +63,58 @@ namespace ams::crypto {
|
||||||
}
|
}
|
||||||
|
|
||||||
void UpdateLabel(const void *data, size_t size) {
|
void UpdateLabel(const void *data, size_t size) {
|
||||||
AMS_ASSERT(this->state == State::Initialized);
|
AMS_ASSERT(m_state == State::Initialized);
|
||||||
|
|
||||||
this->hash.Update(data, size);
|
m_hash.Update(data, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetLabelDigest(const void *digest, size_t digest_size) {
|
void SetLabelDigest(const void *digest, size_t digest_size) {
|
||||||
AMS_ASSERT(this->state == State::Initialized);
|
AMS_ASSERT(m_state == State::Initialized);
|
||||||
AMS_ABORT_UNLESS(digest_size == sizeof(this->label_digest));
|
AMS_ABORT_UNLESS(digest_size == sizeof(m_label_digest));
|
||||||
|
|
||||||
std::memcpy(this->label_digest, digest, digest_size);
|
std::memcpy(m_label_digest, digest, digest_size);
|
||||||
this->set_label_digest = true;
|
m_set_label_digest = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Decrypt(void *dst, size_t dst_size, const void *src, size_t src_size) {
|
size_t Decrypt(void *dst, size_t dst_size, const void *src, size_t src_size) {
|
||||||
AMS_ASSERT(this->state == State::Initialized);
|
AMS_ASSERT(m_state == State::Initialized);
|
||||||
|
|
||||||
impl::RsaOaepImpl<Hash> impl;
|
impl::RsaOaepImpl<Hash> impl;
|
||||||
u8 message[BlockSize];
|
u8 message[BlockSize];
|
||||||
ON_SCOPE_EXIT { ClearMemory(message, sizeof(message)); };
|
ON_SCOPE_EXIT { ClearMemory(message, sizeof(message)); };
|
||||||
|
|
||||||
if (!this->calculator.ExpMod(message, src, src_size)) {
|
if (!m_calculator.ExpMod(message, src, src_size)) {
|
||||||
std::memset(dst, 0, dst_size);
|
std::memset(dst, 0, dst_size);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this->set_label_digest) {
|
if (!m_set_label_digest) {
|
||||||
this->hash.GetHash(this->label_digest, sizeof(this->label_digest));
|
m_hash.GetHash(m_label_digest, sizeof(m_label_digest));
|
||||||
}
|
}
|
||||||
|
|
||||||
ON_SCOPE_EXIT { this->state = State::Done; };
|
ON_SCOPE_EXIT { m_state = State::Done; };
|
||||||
|
|
||||||
return impl.Decode(dst, dst_size, this->label_digest, sizeof(this->label_digest), message, sizeof(message));
|
return impl.Decode(dst, dst_size, m_label_digest, sizeof(m_label_digest), message, sizeof(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Decrypt(void *dst, size_t dst_size, const void *src, size_t src_size, void *work_buf, size_t work_buf_size) {
|
size_t Decrypt(void *dst, size_t dst_size, const void *src, size_t src_size, void *work_buf, size_t work_buf_size) {
|
||||||
AMS_ASSERT(this->state == State::Initialized);
|
AMS_ASSERT(m_state == State::Initialized);
|
||||||
ON_SCOPE_EXIT { this->state = State::Done; };
|
ON_SCOPE_EXIT { m_state = State::Done; };
|
||||||
|
|
||||||
impl::RsaOaepImpl<Hash> impl;
|
impl::RsaOaepImpl<Hash> impl;
|
||||||
u8 message[BlockSize];
|
u8 message[BlockSize];
|
||||||
ON_SCOPE_EXIT { ClearMemory(message, sizeof(message)); };
|
ON_SCOPE_EXIT { ClearMemory(message, sizeof(message)); };
|
||||||
|
|
||||||
if (!this->calculator.ExpMod(message, src, src_size, work_buf, work_buf_size)) {
|
if (!m_calculator.ExpMod(message, src, src_size, work_buf, work_buf_size)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this->set_label_digest) {
|
if (!m_set_label_digest) {
|
||||||
this->hash.GetHash(this->label_digest, sizeof(this->label_digest));
|
m_hash.GetHash(m_label_digest, sizeof(m_label_digest));
|
||||||
this->set_label_digest = true;
|
m_set_label_digest = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return impl.Decode(dst, dst_size, this->label_digest, sizeof(this->label_digest), message, sizeof(message));
|
return impl.Decode(dst, dst_size, m_label_digest, sizeof(m_label_digest), message, sizeof(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t Decrypt(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 *lab, size_t lab_size) {
|
static size_t Decrypt(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 *lab, size_t lab_size) {
|
||||||
|
|
|
@ -39,23 +39,23 @@ namespace ams::crypto {
|
||||||
Done,
|
Done,
|
||||||
};
|
};
|
||||||
private:
|
private:
|
||||||
RsaCalculator<ModulusSize, MaximumExponentSize> calculator;
|
RsaCalculator<ModulusSize, MaximumExponentSize> m_calculator;
|
||||||
Hash hash;
|
Hash m_hash;
|
||||||
bool set_label_digest;
|
bool m_set_label_digest;
|
||||||
u8 label_digest[HashSize];
|
u8 m_label_digest[HashSize];
|
||||||
State state;
|
State m_state;
|
||||||
public:
|
public:
|
||||||
RsaOaepEncryptor() : set_label_digest(false), state(State::None) { std::memset(this->label_digest, 0, sizeof(this->label_digest)); }
|
RsaOaepEncryptor() : m_set_label_digest(false), m_state(State::None) { std::memset(m_label_digest, 0, sizeof(m_label_digest)); }
|
||||||
|
|
||||||
~RsaOaepEncryptor() {
|
~RsaOaepEncryptor() {
|
||||||
ClearMemory(this->label_digest, sizeof(this->label_digest));
|
ClearMemory(m_label_digest, sizeof(m_label_digest));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Initialize(const void *mod, size_t mod_size, const void *exp, size_t exp_size) {
|
bool Initialize(const void *mod, size_t mod_size, const void *exp, size_t exp_size) {
|
||||||
this->hash.Initialize();
|
m_hash.Initialize();
|
||||||
this->set_label_digest = false;
|
m_set_label_digest = false;
|
||||||
if (this->calculator.Initialize(mod, mod_size, exp, exp_size)) {
|
if (m_calculator.Initialize(mod, mod_size, exp, exp_size)) {
|
||||||
this->state = State::Initialized;
|
m_state = State::Initialized;
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
@ -63,54 +63,54 @@ namespace ams::crypto {
|
||||||
}
|
}
|
||||||
|
|
||||||
void UpdateLabel(const void *data, size_t size) {
|
void UpdateLabel(const void *data, size_t size) {
|
||||||
AMS_ASSERT(this->state == State::Initialized);
|
AMS_ASSERT(m_state == State::Initialized);
|
||||||
|
|
||||||
this->hash.Update(data, size);
|
m_hash.Update(data, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetLabelDigest(const void *digest, size_t digest_size) {
|
void SetLabelDigest(const void *digest, size_t digest_size) {
|
||||||
AMS_ASSERT(this->state == State::Initialized);
|
AMS_ASSERT(m_state == State::Initialized);
|
||||||
AMS_ABORT_UNLESS(digest_size == sizeof(this->label_digest));
|
AMS_ABORT_UNLESS(digest_size == sizeof(m_label_digest));
|
||||||
|
|
||||||
std::memcpy(this->label_digest, digest, digest_size);
|
std::memcpy(m_label_digest, digest, digest_size);
|
||||||
this->set_label_digest = true;
|
m_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) {
|
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);
|
AMS_ASSERT(m_state == State::Initialized);
|
||||||
|
|
||||||
impl::RsaOaepImpl<Hash> impl;
|
impl::RsaOaepImpl<Hash> impl;
|
||||||
if (!this->set_label_digest) {
|
if (!m_set_label_digest) {
|
||||||
this->hash.GetHash(this->label_digest, sizeof(this->label_digest));
|
m_hash.GetHash(m_label_digest, sizeof(m_label_digest));
|
||||||
}
|
}
|
||||||
|
|
||||||
impl.Encode(dst, dst_size, this->label_digest, sizeof(this->label_digest), src, src_size, salt, salt_size);
|
impl.Encode(dst, dst_size, m_label_digest, sizeof(m_label_digest), src, src_size, salt, salt_size);
|
||||||
|
|
||||||
if (!this->calculator.ExpMod(dst, dst, dst_size)) {
|
if (!m_calculator.ExpMod(dst, dst, dst_size)) {
|
||||||
std::memset(dst, 0, dst_size);
|
std::memset(dst, 0, dst_size);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->state = State::Done;
|
m_state = State::Done;
|
||||||
return true;
|
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) {
|
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);
|
AMS_ASSERT(m_state == State::Initialized);
|
||||||
|
|
||||||
impl::RsaOaepImpl<Hash> impl;
|
impl::RsaOaepImpl<Hash> impl;
|
||||||
if (!this->set_label_digest) {
|
if (!m_set_label_digest) {
|
||||||
this->hash.GetHash(this->label_digest, sizeof(this->label_digest));
|
m_hash.GetHash(m_label_digest, sizeof(m_label_digest));
|
||||||
}
|
}
|
||||||
|
|
||||||
impl.Encode(dst, dst_size, this->label_digest, sizeof(this->label_digest), src, src_size, salt, salt_size);
|
impl.Encode(dst, dst_size, m_label_digest, sizeof(m_label_digest), src, src_size, salt, salt_size);
|
||||||
|
|
||||||
if (!this->calculator.ExpMod(dst, dst, dst_size, work, work_size)) {
|
if (!m_calculator.ExpMod(dst, dst, dst_size, work, work_size)) {
|
||||||
std::memset(dst, 0, dst_size);
|
std::memset(dst, 0, dst_size);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->state = State::Done;
|
m_state = State::Done;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,17 +41,17 @@ namespace ams::crypto {
|
||||||
Done,
|
Done,
|
||||||
};
|
};
|
||||||
private:
|
private:
|
||||||
RsaCalculator<ModulusSize, MaximumExponentSize> calculator;
|
RsaCalculator<ModulusSize, MaximumExponentSize> m_calculator;
|
||||||
Hash hash;
|
Hash m_hash;
|
||||||
State state;
|
State m_state;
|
||||||
public:
|
public:
|
||||||
RsaPssVerifier() : state(State::None) { /* ... */ }
|
RsaPssVerifier() : m_state(State::None) { /* ... */ }
|
||||||
~RsaPssVerifier() { }
|
~RsaPssVerifier() { }
|
||||||
|
|
||||||
bool Initialize(const void *mod, size_t mod_size, const void *exp, size_t exp_size) {
|
bool Initialize(const void *mod, size_t mod_size, const void *exp, size_t exp_size) {
|
||||||
this->hash.Initialize();
|
m_hash.Initialize();
|
||||||
if (this->calculator.Initialize(mod, mod_size, exp, exp_size)) {
|
if (m_calculator.Initialize(mod, mod_size, exp, exp_size)) {
|
||||||
this->state = State::Initialized;
|
m_state = State::Initialized;
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
@ -59,62 +59,62 @@ namespace ams::crypto {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Update(const void *data, size_t size) {
|
void Update(const void *data, size_t size) {
|
||||||
return this->hash.Update(data, size);
|
return m_hash.Update(data, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Verify(const void *signature, size_t size) {
|
bool Verify(const void *signature, size_t size) {
|
||||||
AMS_ASSERT(this->state == State::Initialized);
|
AMS_ASSERT(m_state == State::Initialized);
|
||||||
AMS_ASSERT(size == SignatureSize);
|
AMS_ASSERT(size == SignatureSize);
|
||||||
AMS_UNUSED(size);
|
AMS_UNUSED(size);
|
||||||
ON_SCOPE_EXIT { this->state = State::Done; };
|
ON_SCOPE_EXIT { m_state = State::Done; };
|
||||||
|
|
||||||
impl::RsaPssImpl<Hash> impl;
|
impl::RsaPssImpl<Hash> impl;
|
||||||
u8 message[SignatureSize];
|
u8 message[SignatureSize];
|
||||||
ON_SCOPE_EXIT { ClearMemory(message, sizeof(message)); };
|
ON_SCOPE_EXIT { ClearMemory(message, sizeof(message)); };
|
||||||
|
|
||||||
if (!this->calculator.ExpMod(message, signature, SignatureSize)) {
|
if (!m_calculator.ExpMod(message, signature, SignatureSize)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
u8 calc_hash[Hash::HashSize];
|
u8 calc_hash[Hash::HashSize];
|
||||||
this->hash.GetHash(calc_hash, sizeof(calc_hash));
|
m_hash.GetHash(calc_hash, sizeof(calc_hash));
|
||||||
ON_SCOPE_EXIT { ClearMemory(calc_hash, sizeof(calc_hash)); };
|
ON_SCOPE_EXIT { ClearMemory(calc_hash, sizeof(calc_hash)); };
|
||||||
|
|
||||||
return impl.Verify(message, sizeof(message), calc_hash, sizeof(calc_hash));
|
return impl.Verify(message, sizeof(message), calc_hash, sizeof(calc_hash));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Verify(const void *signature, size_t size, void *work_buf, size_t work_buf_size) {
|
bool Verify(const void *signature, size_t size, void *work_buf, size_t work_buf_size) {
|
||||||
AMS_ASSERT(this->state == State::Initialized);
|
AMS_ASSERT(m_state == State::Initialized);
|
||||||
AMS_ASSERT(size == SignatureSize);
|
AMS_ASSERT(size == SignatureSize);
|
||||||
AMS_UNUSED(size);
|
AMS_UNUSED(size);
|
||||||
ON_SCOPE_EXIT { this->state = State::Done; };
|
ON_SCOPE_EXIT { m_state = State::Done; };
|
||||||
|
|
||||||
impl::RsaPssImpl<Hash> impl;
|
impl::RsaPssImpl<Hash> impl;
|
||||||
u8 message[SignatureSize];
|
u8 message[SignatureSize];
|
||||||
ON_SCOPE_EXIT { ClearMemory(message, sizeof(message)); };
|
ON_SCOPE_EXIT { ClearMemory(message, sizeof(message)); };
|
||||||
|
|
||||||
if (!this->calculator.ExpMod(message, signature, SignatureSize, work_buf, work_buf_size)) {
|
if (!m_calculator.ExpMod(message, signature, SignatureSize, work_buf, work_buf_size)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
u8 calc_hash[Hash::HashSize];
|
u8 calc_hash[Hash::HashSize];
|
||||||
this->hash.GetHash(calc_hash, sizeof(calc_hash));
|
m_hash.GetHash(calc_hash, sizeof(calc_hash));
|
||||||
ON_SCOPE_EXIT { ClearMemory(calc_hash, sizeof(calc_hash)); };
|
ON_SCOPE_EXIT { ClearMemory(calc_hash, sizeof(calc_hash)); };
|
||||||
|
|
||||||
return impl.Verify(message, sizeof(message), calc_hash, sizeof(calc_hash));
|
return impl.Verify(message, sizeof(message), calc_hash, sizeof(calc_hash));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool VerifyWithHash(const void *signature, size_t size, const void *hash, size_t hash_size) {
|
bool VerifyWithHash(const void *signature, size_t size, const void *hash, size_t hash_size) {
|
||||||
AMS_ASSERT(this->state == State::Initialized);
|
AMS_ASSERT(m_state == State::Initialized);
|
||||||
AMS_ASSERT(size == SignatureSize);
|
AMS_ASSERT(size == SignatureSize);
|
||||||
AMS_UNUSED(size);
|
AMS_UNUSED(size);
|
||||||
ON_SCOPE_EXIT { this->state = State::Done; };
|
ON_SCOPE_EXIT { m_state = State::Done; };
|
||||||
|
|
||||||
impl::RsaPssImpl<Hash> impl;
|
impl::RsaPssImpl<Hash> impl;
|
||||||
u8 message[SignatureSize];
|
u8 message[SignatureSize];
|
||||||
ON_SCOPE_EXIT { ClearMemory(message, sizeof(message)); };
|
ON_SCOPE_EXIT { ClearMemory(message, sizeof(message)); };
|
||||||
|
|
||||||
if (!this->calculator.ExpMod(message, signature, SignatureSize)) {
|
if (!m_calculator.ExpMod(message, signature, SignatureSize)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,16 +122,16 @@ namespace ams::crypto {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool VerifyWithHash(const void *signature, size_t size, const void *hash, size_t hash_size, void *work_buf, size_t work_buf_size) {
|
bool VerifyWithHash(const void *signature, size_t size, const void *hash, size_t hash_size, void *work_buf, size_t work_buf_size) {
|
||||||
AMS_ASSERT(this->state == State::Initialized);
|
AMS_ASSERT(m_state == State::Initialized);
|
||||||
AMS_ASSERT(size == SignatureSize);
|
AMS_ASSERT(size == SignatureSize);
|
||||||
AMS_UNUSED(size);
|
AMS_UNUSED(size);
|
||||||
ON_SCOPE_EXIT { this->state = State::Done; };
|
ON_SCOPE_EXIT { m_state = State::Done; };
|
||||||
|
|
||||||
impl::RsaPssImpl<Hash> impl;
|
impl::RsaPssImpl<Hash> impl;
|
||||||
u8 message[SignatureSize];
|
u8 message[SignatureSize];
|
||||||
ON_SCOPE_EXIT { ClearMemory(message, sizeof(message)); };
|
ON_SCOPE_EXIT { ClearMemory(message, sizeof(message)); };
|
||||||
|
|
||||||
if (!this->calculator.ExpMod(message, signature, SignatureSize, work_buf, work_buf_size)) {
|
if (!m_calculator.ExpMod(message, signature, SignatureSize, work_buf, work_buf_size)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,20 +41,20 @@ namespace ams::crypto {
|
||||||
};
|
};
|
||||||
static constexpr size_t Asn1IdentifierSize = util::size(Asn1Identifier);
|
static constexpr size_t Asn1IdentifierSize = util::size(Asn1Identifier);
|
||||||
private:
|
private:
|
||||||
Impl impl;
|
Impl m_impl;
|
||||||
public:
|
public:
|
||||||
Sha1Generator() { /* ... */ }
|
Sha1Generator() { /* ... */ }
|
||||||
|
|
||||||
void Initialize() {
|
void Initialize() {
|
||||||
this->impl.Initialize();
|
m_impl.Initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Update(const void *data, size_t size) {
|
void Update(const void *data, size_t size) {
|
||||||
this->impl.Update(data, size);
|
m_impl.Update(data, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GetHash(void *dst, size_t size) {
|
void GetHash(void *dst, size_t size) {
|
||||||
this->impl.GetHash(dst, size);
|
m_impl.GetHash(dst, size);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -46,36 +46,36 @@ namespace ams::crypto {
|
||||||
};
|
};
|
||||||
static constexpr size_t Asn1IdentifierSize = util::size(Asn1Identifier);
|
static constexpr size_t Asn1IdentifierSize = util::size(Asn1Identifier);
|
||||||
private:
|
private:
|
||||||
Impl impl;
|
Impl m_impl;
|
||||||
public:
|
public:
|
||||||
Sha256Generator() { /* ... */ }
|
Sha256Generator() { /* ... */ }
|
||||||
|
|
||||||
void Initialize() {
|
void Initialize() {
|
||||||
this->impl.Initialize();
|
m_impl.Initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Update(const void *data, size_t size) {
|
void Update(const void *data, size_t size) {
|
||||||
this->impl.Update(data, size);
|
m_impl.Update(data, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GetHash(void *dst, size_t size) {
|
void GetHash(void *dst, size_t size) {
|
||||||
this->impl.GetHash(dst, size);
|
m_impl.GetHash(dst, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InitializeWithContext(const Sha256Context *context) {
|
void InitializeWithContext(const Sha256Context *context) {
|
||||||
this->impl.InitializeWithContext(context);
|
m_impl.InitializeWithContext(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t GetContext(Sha256Context *context) const {
|
size_t GetContext(Sha256Context *context) const {
|
||||||
return this->impl.GetContext(context);
|
return m_impl.GetContext(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t GetBufferedDataSize() const {
|
size_t GetBufferedDataSize() const {
|
||||||
return this->impl.GetBufferedDataSize();
|
return m_impl.GetBufferedDataSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
void GetBufferedData(void *dst, size_t dst_size) const {
|
void GetBufferedData(void *dst, size_t dst_size) const {
|
||||||
return this->impl.GetBufferedData(dst, dst_size);
|
return m_impl.GetBufferedData(dst, dst_size);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -34,21 +34,21 @@ namespace ams::crypto {
|
||||||
static constexpr size_t BlockSize = Impl::BlockSize;
|
static constexpr size_t BlockSize = Impl::BlockSize;
|
||||||
static constexpr size_t IvSize = Impl::IvSize;
|
static constexpr size_t IvSize = Impl::IvSize;
|
||||||
private:
|
private:
|
||||||
Impl impl;
|
Impl m_impl;
|
||||||
public:
|
public:
|
||||||
XtsDecryptor() { /* ... */ }
|
XtsDecryptor() { /* ... */ }
|
||||||
|
|
||||||
template<typename BlockCipher2>
|
template<typename BlockCipher2>
|
||||||
void Initialize(const BlockCipher *cipher1, const BlockCipher2 *cipher2, const void *iv, size_t iv_size) {
|
void Initialize(const BlockCipher *cipher1, const BlockCipher2 *cipher2, const void *iv, size_t iv_size) {
|
||||||
this->impl.InitializeDecryption(cipher1, cipher2, iv, iv_size);
|
m_impl.InitializeDecryption(cipher1, cipher2, iv, iv_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Update(void *dst, size_t dst_size, const void *src, size_t src_size) {
|
size_t Update(void *dst, size_t dst_size, const void *src, size_t src_size) {
|
||||||
return this->impl.template Update<BlockCipher>(dst, dst_size, src, src_size);
|
return m_impl.template Update<BlockCipher>(dst, dst_size, src, src_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Finalize(void *dst, size_t dst_size) {
|
size_t Finalize(void *dst, size_t dst_size) {
|
||||||
return this->impl.FinalizeDecryption(dst, dst_size);
|
return m_impl.FinalizeDecryption(dst, dst_size);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -34,21 +34,21 @@ namespace ams::crypto {
|
||||||
static constexpr size_t BlockSize = Impl::BlockSize;
|
static constexpr size_t BlockSize = Impl::BlockSize;
|
||||||
static constexpr size_t IvSize = Impl::IvSize;
|
static constexpr size_t IvSize = Impl::IvSize;
|
||||||
private:
|
private:
|
||||||
Impl impl;
|
Impl m_impl;
|
||||||
public:
|
public:
|
||||||
XtsEncryptor() { /* ... */ }
|
XtsEncryptor() { /* ... */ }
|
||||||
|
|
||||||
template<typename BlockCipher2>
|
template<typename BlockCipher2>
|
||||||
void Initialize(const BlockCipher *cipher1, const BlockCipher2 *cipher2, const void *iv, size_t iv_size) {
|
void Initialize(const BlockCipher *cipher1, const BlockCipher2 *cipher2, const void *iv, size_t iv_size) {
|
||||||
this->impl.InitializeEncryption(cipher1, cipher2, iv, iv_size);
|
m_impl.InitializeEncryption(cipher1, cipher2, iv, iv_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Update(void *dst, size_t dst_size, const void *src, size_t src_size) {
|
size_t Update(void *dst, size_t dst_size, const void *src, size_t src_size) {
|
||||||
return this->impl.template Update<BlockCipher>(dst, dst_size, src, src_size);
|
return m_impl.template Update<BlockCipher>(dst, dst_size, src, src_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Finalize(void *dst, size_t dst_size) {
|
size_t Finalize(void *dst, size_t dst_size) {
|
||||||
return this->impl.FinalizeEncryption(dst, dst_size);
|
return m_impl.FinalizeEncryption(dst, dst_size);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -31,10 +31,10 @@ namespace ams::crypto::impl {
|
||||||
static constexpr size_t RoundKeySize = BlockSize * (RoundCount + 1);
|
static constexpr size_t RoundKeySize = BlockSize * (RoundCount + 1);
|
||||||
private:
|
private:
|
||||||
#ifdef ATMOSPHERE_IS_EXOSPHERE
|
#ifdef ATMOSPHERE_IS_EXOSPHERE
|
||||||
int slot;
|
int m_slot;
|
||||||
#endif
|
#endif
|
||||||
#ifdef ATMOSPHERE_IS_STRATOSPHERE
|
#ifdef ATMOSPHERE_IS_STRATOSPHERE
|
||||||
u32 round_keys[RoundKeySize / sizeof(u32)];
|
u32 m_round_keys[RoundKeySize / sizeof(u32)];
|
||||||
#endif
|
#endif
|
||||||
public:
|
public:
|
||||||
~AesImpl();
|
~AesImpl();
|
||||||
|
@ -45,7 +45,7 @@ namespace ams::crypto::impl {
|
||||||
|
|
||||||
#ifdef ATMOSPHERE_IS_STRATOSPHERE
|
#ifdef ATMOSPHERE_IS_STRATOSPHERE
|
||||||
const u8 *GetRoundKey() const {
|
const u8 *GetRoundKey() const {
|
||||||
return reinterpret_cast<const u8 *>(this->round_keys);
|
return reinterpret_cast<const u8 *>(m_round_keys);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
|
@ -46,43 +46,43 @@ namespace ams::crypto::impl {
|
||||||
private:
|
private:
|
||||||
friend class WordAllocator;
|
friend class WordAllocator;
|
||||||
private:
|
private:
|
||||||
WordAllocator *allocator;
|
WordAllocator *m_allocator;
|
||||||
Word *buffer;
|
Word *m_buffer;
|
||||||
size_t count;
|
size_t m_count;
|
||||||
private:
|
private:
|
||||||
constexpr ALWAYS_INLINE Allocation(WordAllocator *a, Word *w, size_t c) : allocator(a), buffer(w), count(c) { /* ... */ }
|
constexpr ALWAYS_INLINE Allocation(WordAllocator *a, Word *w, size_t c) : m_allocator(a), m_buffer(w), m_count(c) { /* ... */ }
|
||||||
public:
|
public:
|
||||||
ALWAYS_INLINE ~Allocation() { if (allocator) { allocator->Free(this->buffer, this->count); } }
|
ALWAYS_INLINE ~Allocation() { if (m_allocator) { m_allocator->Free(m_buffer, m_count); } }
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE Word *GetBuffer() const { return this->buffer; }
|
constexpr ALWAYS_INLINE Word *GetBuffer() const { return m_buffer; }
|
||||||
constexpr ALWAYS_INLINE size_t GetCount() const { return this->count; }
|
constexpr ALWAYS_INLINE size_t GetCount() const { return m_count; }
|
||||||
constexpr ALWAYS_INLINE bool IsValid() const { return this->buffer != nullptr; }
|
constexpr ALWAYS_INLINE bool IsValid() const { return m_buffer != nullptr; }
|
||||||
};
|
};
|
||||||
|
|
||||||
friend class Allocation;
|
friend class Allocation;
|
||||||
private:
|
private:
|
||||||
Word *buffer;
|
Word *m_buffer;
|
||||||
size_t count;
|
size_t m_count;
|
||||||
size_t max_count;
|
size_t m_max_count;
|
||||||
size_t min_count;
|
size_t m_min_count;
|
||||||
private:
|
private:
|
||||||
ALWAYS_INLINE void Free(void *words, size_t num) {
|
ALWAYS_INLINE void Free(void *words, size_t num) {
|
||||||
this->buffer -= num;
|
m_buffer -= num;
|
||||||
this->count += num;
|
m_count += num;
|
||||||
|
|
||||||
AMS_ASSERT(words == this->buffer);
|
AMS_ASSERT(words == m_buffer);
|
||||||
AMS_UNUSED(words);
|
AMS_UNUSED(words);
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
constexpr ALWAYS_INLINE WordAllocator(Word *buf, size_t c) : buffer(buf), count(c), max_count(c), min_count(c) { /* ... */ }
|
constexpr ALWAYS_INLINE WordAllocator(Word *buf, size_t c) : m_buffer(buf), m_count(c), m_max_count(c), m_min_count(c) { /* ... */ }
|
||||||
|
|
||||||
ALWAYS_INLINE Allocation Allocate(size_t num) {
|
ALWAYS_INLINE Allocation Allocate(size_t num) {
|
||||||
if (num <= this->count) {
|
if (num <= m_count) {
|
||||||
Word *allocated = this->buffer;
|
Word *allocated = m_buffer;
|
||||||
|
|
||||||
this->buffer += num;
|
m_buffer += num;
|
||||||
this->count -= num;
|
m_count -= num;
|
||||||
this->min_count = std::min(this->count, this->min_count);
|
m_min_count = std::min(m_count, m_min_count);
|
||||||
|
|
||||||
return Allocation(this, allocated, num);
|
return Allocation(this, allocated, num);
|
||||||
} else {
|
} else {
|
||||||
|
@ -91,23 +91,23 @@ namespace ams::crypto::impl {
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE size_t GetMaxUsedSize() const {
|
constexpr ALWAYS_INLINE size_t GetMaxUsedSize() const {
|
||||||
return (this->max_count - this->min_count) * sizeof(Word);
|
return (m_max_count - m_min_count) * sizeof(Word);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
private:
|
private:
|
||||||
Word *words;
|
Word *m_words;
|
||||||
size_t num_words;
|
size_t m_num_words;
|
||||||
size_t max_words;
|
size_t m_max_words;
|
||||||
private:
|
private:
|
||||||
static void ImportImpl(Word *out, size_t out_size, const u8 *src, size_t src_size);
|
static void ImportImpl(Word *out, size_t out_size, const u8 *src, size_t src_size);
|
||||||
static void ExportImpl(u8 *out, size_t out_size, const Word *src, size_t src_size);
|
static void ExportImpl(u8 *out, size_t out_size, const Word *src, size_t src_size);
|
||||||
public:
|
public:
|
||||||
constexpr BigNum() : words(), num_words(), max_words() { /* ... */ }
|
constexpr BigNum() : m_words(), m_num_words(), m_max_words() { /* ... */ }
|
||||||
~BigNum() { /* ... */ }
|
~BigNum() { /* ... */ }
|
||||||
|
|
||||||
constexpr void ReserveStatic(Word *buf, size_t capacity) {
|
constexpr void ReserveStatic(Word *buf, size_t capacity) {
|
||||||
this->words = buf;
|
m_words = buf;
|
||||||
this->max_words = capacity;
|
m_max_words = capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Import(const void *src, size_t src_size);
|
bool Import(const void *src, size_t src_size);
|
||||||
|
@ -116,7 +116,7 @@ namespace ams::crypto::impl {
|
||||||
size_t GetSize() const;
|
size_t GetSize() const;
|
||||||
|
|
||||||
bool IsZero() const {
|
bool IsZero() const {
|
||||||
return this->num_words == 0;
|
return m_num_words == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ExpMod(void *dst, const void *src, size_t size, const BigNum &exp, u32 *work_buf, size_t work_buf_size) const;
|
bool ExpMod(void *dst, const void *src, size_t size, const BigNum &exp, u32 *work_buf, size_t work_buf_size) const;
|
||||||
|
@ -154,10 +154,10 @@ namespace ams::crypto::impl {
|
||||||
static constexpr size_t NumWords = util::AlignUp(NumBits, BitsPerWord) / BitsPerWord;
|
static constexpr size_t NumWords = util::AlignUp(NumBits, BitsPerWord) / BitsPerWord;
|
||||||
static constexpr size_t NumBytes = NumWords * sizeof(Word);
|
static constexpr size_t NumBytes = NumWords * sizeof(Word);
|
||||||
private:
|
private:
|
||||||
Word word_buf[NumWords];
|
Word m_word_buf[NumWords];
|
||||||
public:
|
public:
|
||||||
constexpr StaticBigNum() : word_buf() {
|
constexpr StaticBigNum() : m_word_buf() {
|
||||||
this->ReserveStatic(word_buf, NumWords);
|
this->ReserveStatic(m_word_buf, NumWords);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -37,13 +37,13 @@ namespace ams::crypto::impl {
|
||||||
State_Initialized,
|
State_Initialized,
|
||||||
};
|
};
|
||||||
private:
|
private:
|
||||||
const BlockCipher *block_cipher;
|
const BlockCipher *m_block_cipher;
|
||||||
u8 counter[IvSize];
|
u8 m_counter[IvSize];
|
||||||
u8 encrypted_counter[BlockSize];
|
u8 m_encrypted_counter[BlockSize];
|
||||||
size_t buffer_offset;
|
size_t m_buffer_offset;
|
||||||
State state;
|
State m_state;
|
||||||
public:
|
public:
|
||||||
CtrModeImpl() : state(State_None) { /* ... */ }
|
CtrModeImpl() : m_state(State_None) { /* ... */ }
|
||||||
|
|
||||||
~CtrModeImpl() {
|
~CtrModeImpl() {
|
||||||
ClearMemory(this, sizeof(*this));
|
ClearMemory(this, sizeof(*this));
|
||||||
|
@ -57,8 +57,8 @@ namespace ams::crypto::impl {
|
||||||
AMS_ASSERT(iv_size == IvSize);
|
AMS_ASSERT(iv_size == IvSize);
|
||||||
AMS_ASSERT(offset >= 0);
|
AMS_ASSERT(offset >= 0);
|
||||||
|
|
||||||
this->block_cipher = block_cipher;
|
m_block_cipher = block_cipher;
|
||||||
this->state = State_Initialized;
|
m_state = State_Initialized;
|
||||||
|
|
||||||
this->SwitchMessage(iv, iv_size);
|
this->SwitchMessage(iv, iv_size);
|
||||||
|
|
||||||
|
@ -69,32 +69,32 @@ namespace ams::crypto::impl {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (size_t remaining = static_cast<size_t>(offset % BlockSize); remaining != 0) {
|
if (size_t remaining = static_cast<size_t>(offset % BlockSize); remaining != 0) {
|
||||||
this->block_cipher->EncryptBlock(this->encrypted_counter, sizeof(this->encrypted_counter), this->counter, sizeof(this->counter));
|
m_block_cipher->EncryptBlock(m_encrypted_counter, sizeof(m_encrypted_counter), m_counter, sizeof(m_counter));
|
||||||
this->IncrementCounter();
|
this->IncrementCounter();
|
||||||
|
|
||||||
this->buffer_offset = remaining;
|
m_buffer_offset = remaining;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SwitchMessage(const void *iv, size_t iv_size) {
|
void SwitchMessage(const void *iv, size_t iv_size) {
|
||||||
AMS_ASSERT(this->state == State_Initialized);
|
AMS_ASSERT(m_state == State_Initialized);
|
||||||
AMS_ASSERT(iv_size == IvSize);
|
AMS_ASSERT(iv_size == IvSize);
|
||||||
|
|
||||||
std::memcpy(this->counter, iv, iv_size);
|
std::memcpy(m_counter, iv, iv_size);
|
||||||
this->buffer_offset = 0;
|
m_buffer_offset = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void IncrementCounter() {
|
void IncrementCounter() {
|
||||||
for (s32 i = IvSize - 1; i >= 0; --i) {
|
for (s32 i = IvSize - 1; i >= 0; --i) {
|
||||||
if (++this->counter[i] != 0) {
|
if (++m_counter[i] != 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Update(void *_dst, size_t dst_size, const void *_src, size_t src_size) {
|
size_t Update(void *_dst, size_t dst_size, const void *_src, size_t src_size) {
|
||||||
AMS_ASSERT(this->state == State_Initialized);
|
AMS_ASSERT(m_state == State_Initialized);
|
||||||
AMS_ASSERT(dst_size >= src_size);
|
AMS_ASSERT(dst_size >= src_size);
|
||||||
AMS_UNUSED(dst_size);
|
AMS_UNUSED(dst_size);
|
||||||
|
|
||||||
|
@ -102,10 +102,10 @@ namespace ams::crypto::impl {
|
||||||
const u8 *src = static_cast<const u8 *>(_src);
|
const u8 *src = static_cast<const u8 *>(_src);
|
||||||
size_t remaining = src_size;
|
size_t remaining = src_size;
|
||||||
|
|
||||||
if (this->buffer_offset > 0) {
|
if (m_buffer_offset > 0) {
|
||||||
const size_t xor_size = std::min(BlockSize - this->buffer_offset, remaining);
|
const size_t xor_size = std::min(BlockSize - m_buffer_offset, remaining);
|
||||||
|
|
||||||
const u8 *ctr = this->encrypted_counter + this->buffer_offset;
|
const u8 *ctr = m_encrypted_counter + m_buffer_offset;
|
||||||
for (size_t i = 0; i < xor_size; i++) {
|
for (size_t i = 0; i < xor_size; i++) {
|
||||||
dst[i] = src[i] ^ ctr[i];
|
dst[i] = src[i] ^ ctr[i];
|
||||||
}
|
}
|
||||||
|
@ -113,10 +113,10 @@ namespace ams::crypto::impl {
|
||||||
src += xor_size;
|
src += xor_size;
|
||||||
dst += xor_size;
|
dst += xor_size;
|
||||||
remaining -= xor_size;
|
remaining -= xor_size;
|
||||||
this->buffer_offset += xor_size;
|
m_buffer_offset += xor_size;
|
||||||
|
|
||||||
if (this->buffer_offset == BlockSize) {
|
if (m_buffer_offset == BlockSize) {
|
||||||
this->buffer_offset = 0;
|
m_buffer_offset = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,7 +133,7 @@ namespace ams::crypto::impl {
|
||||||
|
|
||||||
if (remaining > 0) {
|
if (remaining > 0) {
|
||||||
this->ProcessBlock(dst, src, remaining);
|
this->ProcessBlock(dst, src, remaining);
|
||||||
this->buffer_offset = remaining;
|
m_buffer_offset = remaining;
|
||||||
}
|
}
|
||||||
|
|
||||||
return src_size;
|
return src_size;
|
||||||
|
@ -146,18 +146,18 @@ namespace ams::crypto::impl {
|
||||||
u16 acc = 0;
|
u16 acc = 0;
|
||||||
const u8 *block = reinterpret_cast<const u8 *>(_block);
|
const u8 *block = reinterpret_cast<const u8 *>(_block);
|
||||||
for (s32 i = IvSize - 1; i >= 0; --i) {
|
for (s32 i = IvSize - 1; i >= 0; --i) {
|
||||||
acc += (this->counter[i] + block[i]);
|
acc += (m_counter[i] + block[i]);
|
||||||
this->counter[i] = acc & 0xFF;
|
m_counter[i] = acc & 0xFF;
|
||||||
acc >>= 8;
|
acc >>= 8;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProcessBlock(u8 *dst, const u8 *src, size_t src_size) {
|
void ProcessBlock(u8 *dst, const u8 *src, size_t src_size) {
|
||||||
this->block_cipher->EncryptBlock(this->encrypted_counter, BlockSize, this->counter, IvSize);
|
m_block_cipher->EncryptBlock(m_encrypted_counter, BlockSize, m_counter, IvSize);
|
||||||
this->IncrementCounter();
|
this->IncrementCounter();
|
||||||
|
|
||||||
for (size_t i = 0; i < src_size; i++) {
|
for (size_t i = 0; i < src_size; i++) {
|
||||||
dst[i] = src[i] ^ this->encrypted_counter[i];
|
dst[i] = src[i] ^ m_encrypted_counter[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -63,23 +63,23 @@ namespace ams::crypto::impl {
|
||||||
|
|
||||||
using CipherFunction = void (*)(void *dst_block, const void *src_block, const void *ctx);
|
using CipherFunction = void (*)(void *dst_block, const void *src_block, const void *ctx);
|
||||||
private:
|
private:
|
||||||
State state;
|
State m_state;
|
||||||
const BlockCipher *block_cipher;
|
const BlockCipher *m_block_cipher;
|
||||||
CipherFunction cipher_func;
|
CipherFunction m_cipher_func;
|
||||||
u8 pad[sizeof(u64)];
|
u8 m_pad[sizeof(u64)];
|
||||||
Block block_x;
|
Block m_block_x;
|
||||||
Block block_y;
|
Block m_block_y;
|
||||||
Block block_ek;
|
Block m_block_ek;
|
||||||
Block block_ek0;
|
Block m_block_ek0;
|
||||||
Block block_tmp;
|
Block m_block_tmp;
|
||||||
size_t aad_size;
|
size_t m_aad_size;
|
||||||
size_t msg_size;
|
size_t m_msg_size;
|
||||||
u32 aad_remaining;
|
u32 m_aad_remaining;
|
||||||
u32 msg_remaining;
|
u32 m_msg_remaining;
|
||||||
u32 counter;
|
u32 m_counter;
|
||||||
Block h_mult_blocks[16];
|
Block m_h_mult_blocks[16];
|
||||||
public:
|
public:
|
||||||
GcmModeImpl() : state(State_None) { /* ... */ }
|
GcmModeImpl() : m_state(State_None) { /* ... */ }
|
||||||
|
|
||||||
~GcmModeImpl() {
|
~GcmModeImpl() {
|
||||||
ClearMemory(this, sizeof(*this));
|
ClearMemory(this, sizeof(*this));
|
||||||
|
|
|
@ -43,17 +43,17 @@ namespace ams::crypto::impl {
|
||||||
State_Done = 2,
|
State_Done = 2,
|
||||||
};
|
};
|
||||||
private:
|
private:
|
||||||
Hash hash_function;
|
Hash m_hash_function;
|
||||||
u32 key[BlockSize / sizeof(u32)];
|
u32 m_key[BlockSize / sizeof(u32)];
|
||||||
u32 mac[MacSize / sizeof(u32)];
|
u32 m_mac[MacSize / sizeof(u32)];
|
||||||
State state;
|
State m_state;
|
||||||
public:
|
public:
|
||||||
HmacImpl() : state(State_None) { /* ... */ }
|
HmacImpl() : m_state(State_None) { /* ... */ }
|
||||||
~HmacImpl() {
|
~HmacImpl() {
|
||||||
static_assert(offsetof(HmacImpl, hash_function) == 0);
|
static_assert(offsetof(HmacImpl, m_hash_function) == 0);
|
||||||
|
|
||||||
/* Clear everything except for the hash function. */
|
/* Clear everything except for the hash function. */
|
||||||
ClearMemory(reinterpret_cast<u8 *>(this) + sizeof(this->hash_function), sizeof(*this) - sizeof(this->hash_function));
|
ClearMemory(reinterpret_cast<u8 *>(this) + sizeof(m_hash_function), sizeof(*this) - sizeof(m_hash_function));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Initialize(const void *key, size_t key_size);
|
void Initialize(const void *key, size_t key_size);
|
||||||
|
@ -64,64 +64,64 @@ namespace ams::crypto::impl {
|
||||||
template<typename Hash>
|
template<typename Hash>
|
||||||
inline void HmacImpl<Hash>::Initialize(const void *key, size_t key_size) {
|
inline void HmacImpl<Hash>::Initialize(const void *key, size_t key_size) {
|
||||||
/* Clear the key storage. */
|
/* Clear the key storage. */
|
||||||
std::memset(this->key, 0, sizeof(this->key));
|
std::memset(m_key, 0, sizeof(m_key));
|
||||||
|
|
||||||
/* Set the key storage. */
|
/* Set the key storage. */
|
||||||
if (key_size > BlockSize) {
|
if (key_size > BlockSize) {
|
||||||
this->hash_function.Initialize();
|
m_hash_function.Initialize();
|
||||||
this->hash_function.Update(key, key_size);
|
m_hash_function.Update(key, key_size);
|
||||||
this->hash_function.GetHash(this->key, this->hash_function.HashSize);
|
m_hash_function.GetHash(m_key, m_hash_function.HashSize);
|
||||||
} else {
|
} else {
|
||||||
std::memcpy(this->key, key, key_size);
|
std::memcpy(m_key, key, key_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Xor the key with the ipad. */
|
/* Xor the key with the ipad. */
|
||||||
for (size_t i = 0; i < util::size(this->key); i++) {
|
for (size_t i = 0; i < util::size(m_key); i++) {
|
||||||
this->key[i] ^= IpadMagic;
|
m_key[i] ^= IpadMagic;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Update the hash function with the xor'd key. */
|
/* Update the hash function with the xor'd key. */
|
||||||
this->hash_function.Initialize();
|
m_hash_function.Initialize();
|
||||||
this->hash_function.Update(this->key, BlockSize);
|
m_hash_function.Update(m_key, BlockSize);
|
||||||
|
|
||||||
/* Mark initialized. */
|
/* Mark initialized. */
|
||||||
this->state = State_Initialized;
|
m_state = State_Initialized;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Hash>
|
template<typename Hash>
|
||||||
inline void HmacImpl<Hash>::Update(const void *data, size_t data_size) {
|
inline void HmacImpl<Hash>::Update(const void *data, size_t data_size) {
|
||||||
AMS_ASSERT(this->state == State_Initialized);
|
AMS_ASSERT(m_state == State_Initialized);
|
||||||
|
|
||||||
this->hash_function.Update(data, data_size);
|
m_hash_function.Update(data, data_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Hash>
|
template<typename Hash>
|
||||||
inline void HmacImpl<Hash>::GetMac(void *dst, size_t dst_size) {
|
inline void HmacImpl<Hash>::GetMac(void *dst, size_t dst_size) {
|
||||||
AMS_ASSERT(this->state == State_Initialized || this->state == State_Done);
|
AMS_ASSERT(m_state == State_Initialized || m_state == State_Done);
|
||||||
AMS_ASSERT(dst_size >= MacSize);
|
AMS_ASSERT(dst_size >= MacSize);
|
||||||
AMS_UNUSED(dst_size);
|
AMS_UNUSED(dst_size);
|
||||||
|
|
||||||
/* If we're not already finalized, get the final mac. */
|
/* If we're not already finalized, get the final mac. */
|
||||||
if (this->state == State_Initialized) {
|
if (m_state == State_Initialized) {
|
||||||
/* Get the hash of ((key ^ ipad) || data). */
|
/* Get the hash of ((key ^ ipad) || data). */
|
||||||
this->hash_function.GetHash(this->mac, MacSize);
|
m_hash_function.GetHash(m_mac, MacSize);
|
||||||
|
|
||||||
/* Xor the key with the opad. */
|
/* Xor the key with the opad. */
|
||||||
for (size_t i = 0; i < util::size(this->key); i++) {
|
for (size_t i = 0; i < util::size(m_key); i++) {
|
||||||
this->key[i] ^= IpadMagicXorOpadMagic;
|
m_key[i] ^= IpadMagicXorOpadMagic;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Calculate the final mac as hash of ((key ^ opad) || hash((key ^ ipad) || data)) */
|
/* Calculate the final mac as hash of ((key ^ opad) || hash((key ^ ipad) || data)) */
|
||||||
this->hash_function.Initialize();
|
m_hash_function.Initialize();
|
||||||
this->hash_function.Update(this->key, BlockSize);
|
m_hash_function.Update(m_key, BlockSize);
|
||||||
this->hash_function.Update(this->mac, MacSize);
|
m_hash_function.Update(m_mac, MacSize);
|
||||||
this->hash_function.GetHash(this->mac, MacSize);
|
m_hash_function.GetHash(m_mac, MacSize);
|
||||||
|
|
||||||
/* Set our state as done. */
|
/* Set our state as done. */
|
||||||
this->state = State_Done;
|
m_state = State_Done;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::memcpy(dst, this->mac, MacSize);
|
std::memcpy(dst, m_mac, MacSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,12 +37,12 @@ namespace ams::crypto::impl {
|
||||||
bool finalized;
|
bool finalized;
|
||||||
};
|
};
|
||||||
private:
|
private:
|
||||||
State state;
|
State m_state;
|
||||||
public:
|
public:
|
||||||
Sha1Impl() { /* ... */ }
|
Sha1Impl() { /* ... */ }
|
||||||
~Sha1Impl() {
|
~Sha1Impl() {
|
||||||
static_assert(std::is_trivially_destructible<State>::value);
|
static_assert(std::is_trivially_destructible<State>::value);
|
||||||
ClearMemory(std::addressof(this->state), sizeof(this->state));
|
ClearMemory(std::addressof(m_state), sizeof(m_state));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Initialize();
|
void Initialize();
|
||||||
|
|
|
@ -42,12 +42,12 @@ namespace ams::crypto::impl {
|
||||||
bool finalized;
|
bool finalized;
|
||||||
};
|
};
|
||||||
private:
|
private:
|
||||||
State state;
|
State m_state;
|
||||||
public:
|
public:
|
||||||
Sha256Impl() { /* ... */ }
|
Sha256Impl() { /* ... */ }
|
||||||
~Sha256Impl() {
|
~Sha256Impl() {
|
||||||
static_assert(std::is_trivially_destructible<State>::value);
|
static_assert(std::is_trivially_destructible<State>::value);
|
||||||
ClearMemory(std::addressof(this->state), sizeof(this->state));
|
ClearMemory(std::addressof(m_state), sizeof(m_state));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Initialize();
|
void Initialize();
|
||||||
|
@ -57,13 +57,13 @@ namespace ams::crypto::impl {
|
||||||
void InitializeWithContext(const Sha256Context *context);
|
void InitializeWithContext(const Sha256Context *context);
|
||||||
size_t GetContext(Sha256Context *context) const;
|
size_t GetContext(Sha256Context *context) const;
|
||||||
|
|
||||||
size_t GetBufferedDataSize() const { return this->state.num_buffered; }
|
size_t GetBufferedDataSize() const { return m_state.num_buffered; }
|
||||||
|
|
||||||
void GetBufferedData(void *dst, size_t dst_size) const {
|
void GetBufferedData(void *dst, size_t dst_size) const {
|
||||||
AMS_ASSERT(dst_size >= this->GetBufferedDataSize());
|
AMS_ASSERT(dst_size >= this->GetBufferedDataSize());
|
||||||
AMS_UNUSED(dst_size);
|
AMS_UNUSED(dst_size);
|
||||||
|
|
||||||
std::memcpy(dst, this->state.buffer, this->GetBufferedDataSize());
|
std::memcpy(dst, m_state.buffer, this->GetBufferedDataSize());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -38,15 +38,15 @@ namespace ams::crypto::impl {
|
||||||
State_Done
|
State_Done
|
||||||
};
|
};
|
||||||
private:
|
private:
|
||||||
u8 buffer[BlockSize];
|
u8 m_buffer[BlockSize];
|
||||||
u8 tweak[BlockSize];
|
u8 m_tweak[BlockSize];
|
||||||
u8 last_block[BlockSize];
|
u8 m_last_block[BlockSize];
|
||||||
size_t num_buffered;
|
size_t m_num_buffered;
|
||||||
const void *cipher_ctx;
|
const void *m_cipher_ctx;
|
||||||
void (*cipher_func)(void *dst_block, const void *src_block, const void *cipher_ctx);
|
void (*m_cipher_func)(void *dst_block, const void *src_block, const void *cipher_ctx);
|
||||||
State state;
|
State m_state;
|
||||||
public:
|
public:
|
||||||
XtsModeImpl() : num_buffered(0), state(State_None) { /* ... */ }
|
XtsModeImpl() : m_num_buffered(0), m_state(State_None) { /* ... */ }
|
||||||
|
|
||||||
~XtsModeImpl() {
|
~XtsModeImpl() {
|
||||||
ClearMemory(this, sizeof(*this));
|
ClearMemory(this, sizeof(*this));
|
||||||
|
@ -67,10 +67,10 @@ namespace ams::crypto::impl {
|
||||||
AMS_ASSERT(tweak_size == IvSize);
|
AMS_ASSERT(tweak_size == IvSize);
|
||||||
AMS_UNUSED(tweak_size);
|
AMS_UNUSED(tweak_size);
|
||||||
|
|
||||||
cipher->EncryptBlock(this->tweak, IvSize, tweak, IvSize);
|
cipher->EncryptBlock(m_tweak, IvSize, tweak, IvSize);
|
||||||
|
|
||||||
this->num_buffered = 0;
|
m_num_buffered = 0;
|
||||||
this->state = State_Initialized;
|
m_state = State_Initialized;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProcessBlock(u8 *dst, const u8 *src);
|
void ProcessBlock(u8 *dst, const u8 *src);
|
||||||
|
@ -80,8 +80,8 @@ namespace ams::crypto::impl {
|
||||||
static_assert(BlockCipher1::BlockSize == BlockSize);
|
static_assert(BlockCipher1::BlockSize == BlockSize);
|
||||||
static_assert(BlockCipher2::BlockSize == BlockSize);
|
static_assert(BlockCipher2::BlockSize == BlockSize);
|
||||||
|
|
||||||
this->cipher_ctx = cipher1;
|
m_cipher_ctx = cipher1;
|
||||||
this->cipher_func = EncryptBlockCallback<BlockCipher1>;
|
m_cipher_func = EncryptBlockCallback<BlockCipher1>;
|
||||||
|
|
||||||
this->Initialize(cipher2, tweak, tweak_size);
|
this->Initialize(cipher2, tweak, tweak_size);
|
||||||
}
|
}
|
||||||
|
@ -91,8 +91,8 @@ namespace ams::crypto::impl {
|
||||||
static_assert(BlockCipher1::BlockSize == BlockSize);
|
static_assert(BlockCipher1::BlockSize == BlockSize);
|
||||||
static_assert(BlockCipher2::BlockSize == BlockSize);
|
static_assert(BlockCipher2::BlockSize == BlockSize);
|
||||||
|
|
||||||
this->cipher_ctx = cipher1;
|
m_cipher_ctx = cipher1;
|
||||||
this->cipher_func = DecryptBlockCallback<BlockCipher1>;
|
m_cipher_func = DecryptBlockCallback<BlockCipher1>;
|
||||||
|
|
||||||
this->Initialize(cipher2, tweak, tweak_size);
|
this->Initialize(cipher2, tweak, tweak_size);
|
||||||
}
|
}
|
||||||
|
@ -108,7 +108,7 @@ namespace ams::crypto::impl {
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t GetBufferedDataSize() const {
|
size_t GetBufferedDataSize() const {
|
||||||
return this->num_buffered;
|
return m_num_buffered;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr size_t GetBlockSize() const {
|
constexpr size_t GetBlockSize() const {
|
||||||
|
|
|
@ -29,11 +29,11 @@ namespace ams {
|
||||||
/* TODO: Better understand device code components. */
|
/* TODO: Better understand device code components. */
|
||||||
class DeviceCode {
|
class DeviceCode {
|
||||||
private:
|
private:
|
||||||
impl::DeviceCodeType inner_value;
|
impl::DeviceCodeType m_inner_value;
|
||||||
public:
|
public:
|
||||||
constexpr DeviceCode(impl::DeviceCodeType v) : inner_value(v) { /* ... */ }
|
constexpr DeviceCode(impl::DeviceCodeType v) : m_inner_value(v) { /* ... */ }
|
||||||
|
|
||||||
constexpr impl::DeviceCodeType GetInternalValue() const { return this->inner_value; }
|
constexpr impl::DeviceCodeType GetInternalValue() const { return m_inner_value; }
|
||||||
|
|
||||||
constexpr bool operator==(const DeviceCode &rhs) const {
|
constexpr bool operator==(const DeviceCode &rhs) const {
|
||||||
return this->GetInternalValue() == rhs.GetInternalValue();
|
return this->GetInternalValue() == rhs.GetInternalValue();
|
||||||
|
|
|
@ -56,31 +56,31 @@ namespace ams::freebsd {
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class RBEntry {
|
class RBEntry {
|
||||||
private:
|
private:
|
||||||
T *rbe_left = nullptr;
|
T *m_rbe_left = nullptr;
|
||||||
T *rbe_right = nullptr;
|
T *m_rbe_right = nullptr;
|
||||||
T *rbe_parent = nullptr;
|
T *m_rbe_parent = nullptr;
|
||||||
RBColor rbe_color = RBColor::RB_BLACK;
|
RBColor m_rbe_color = RBColor::RB_BLACK;
|
||||||
public:
|
public:
|
||||||
[[nodiscard]] constexpr ALWAYS_INLINE T *Left() { return this->rbe_left; }
|
[[nodiscard]] constexpr ALWAYS_INLINE T *Left() { return m_rbe_left; }
|
||||||
[[nodiscard]] constexpr ALWAYS_INLINE const T *Left() const { return this->rbe_left; }
|
[[nodiscard]] constexpr ALWAYS_INLINE const T *Left() const { return m_rbe_left; }
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE void SetLeft(T *e) { this->rbe_left = e; }
|
constexpr ALWAYS_INLINE void SetLeft(T *e) { m_rbe_left = e; }
|
||||||
|
|
||||||
[[nodiscard]] constexpr ALWAYS_INLINE T *Right() { return this->rbe_right; }
|
[[nodiscard]] constexpr ALWAYS_INLINE T *Right() { return m_rbe_right; }
|
||||||
[[nodiscard]] constexpr ALWAYS_INLINE const T *Right() const { return this->rbe_right; }
|
[[nodiscard]] constexpr ALWAYS_INLINE const T *Right() const { return m_rbe_right; }
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE void SetRight(T *e) { this->rbe_right = e; }
|
constexpr ALWAYS_INLINE void SetRight(T *e) { m_rbe_right = e; }
|
||||||
|
|
||||||
[[nodiscard]] constexpr ALWAYS_INLINE T *Parent() { return this->rbe_parent; }
|
[[nodiscard]] constexpr ALWAYS_INLINE T *Parent() { return m_rbe_parent; }
|
||||||
[[nodiscard]] constexpr ALWAYS_INLINE const T *Parent() const { return this->rbe_parent; }
|
[[nodiscard]] constexpr ALWAYS_INLINE const T *Parent() const { return m_rbe_parent; }
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE void SetParent(T *e) { this->rbe_parent = e; }
|
constexpr ALWAYS_INLINE void SetParent(T *e) { m_rbe_parent = e; }
|
||||||
|
|
||||||
[[nodiscard]] constexpr ALWAYS_INLINE bool IsBlack() const { return this->rbe_color == RBColor::RB_BLACK; }
|
[[nodiscard]] constexpr ALWAYS_INLINE bool IsBlack() const { return m_rbe_color == RBColor::RB_BLACK; }
|
||||||
[[nodiscard]] constexpr ALWAYS_INLINE bool IsRed() const { return this->rbe_color == RBColor::RB_RED; }
|
[[nodiscard]] constexpr ALWAYS_INLINE bool IsRed() const { return m_rbe_color == RBColor::RB_RED; }
|
||||||
[[nodiscard]] constexpr ALWAYS_INLINE RBColor Color() const { return this->rbe_color; }
|
[[nodiscard]] constexpr ALWAYS_INLINE RBColor Color() const { return m_rbe_color; }
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE void SetColor(RBColor c) { this->rbe_color = c; }
|
constexpr ALWAYS_INLINE void SetColor(RBColor c) { m_rbe_color = c; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T> struct CheckRBEntry { static constexpr bool value = false; };
|
template<typename T> struct CheckRBEntry { static constexpr bool value = false; };
|
||||||
|
@ -98,11 +98,11 @@ namespace ams::freebsd {
|
||||||
template<typename T> requires HasRBEntry<T>
|
template<typename T> requires HasRBEntry<T>
|
||||||
class RBHead {
|
class RBHead {
|
||||||
private:
|
private:
|
||||||
T *rbh_root = nullptr;
|
T *m_rbh_root = nullptr;
|
||||||
public:
|
public:
|
||||||
[[nodiscard]] constexpr ALWAYS_INLINE T *Root() { return this->rbh_root; }
|
[[nodiscard]] constexpr ALWAYS_INLINE T *Root() { return m_rbh_root; }
|
||||||
[[nodiscard]] constexpr ALWAYS_INLINE const T *Root() const { return this->rbh_root; }
|
[[nodiscard]] constexpr ALWAYS_INLINE const T *Root() const { return m_rbh_root; }
|
||||||
constexpr ALWAYS_INLINE void SetRoot(T *root) { this->rbh_root = root; }
|
constexpr ALWAYS_INLINE void SetRoot(T *root) { m_rbh_root = root; }
|
||||||
|
|
||||||
[[nodiscard]] constexpr ALWAYS_INLINE bool IsEmpty() const { return this->Root() == nullptr; }
|
[[nodiscard]] constexpr ALWAYS_INLINE bool IsEmpty() const { return this->Root() == nullptr; }
|
||||||
};
|
};
|
||||||
|
|
|
@ -24,7 +24,7 @@ namespace ams {
|
||||||
|
|
||||||
struct TimeSpanType {
|
struct TimeSpanType {
|
||||||
public:
|
public:
|
||||||
s64 ns;
|
s64 _ns;
|
||||||
public:
|
public:
|
||||||
static constexpr ALWAYS_INLINE TimeSpanType FromNanoSeconds(s64 ns) { return {ns}; }
|
static constexpr ALWAYS_INLINE TimeSpanType FromNanoSeconds(s64 ns) { return {ns}; }
|
||||||
static constexpr ALWAYS_INLINE TimeSpanType FromMicroSeconds(s64 ms) { return FromNanoSeconds(ms * INT64_C(1000)); }
|
static constexpr ALWAYS_INLINE TimeSpanType FromMicroSeconds(s64 ms) { return FromNanoSeconds(ms * INT64_C(1000)); }
|
||||||
|
@ -34,7 +34,7 @@ namespace ams {
|
||||||
static constexpr ALWAYS_INLINE TimeSpanType FromHours(s64 h) { return FromMinutes(h * INT64_C(60)); }
|
static constexpr ALWAYS_INLINE TimeSpanType FromHours(s64 h) { return FromMinutes(h * INT64_C(60)); }
|
||||||
static constexpr ALWAYS_INLINE TimeSpanType FromDays(s64 d) { return FromHours(d * INT64_C(24)); }
|
static constexpr ALWAYS_INLINE TimeSpanType FromDays(s64 d) { return FromHours(d * INT64_C(24)); }
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE s64 GetNanoSeconds() const { return this->ns; }
|
constexpr ALWAYS_INLINE s64 GetNanoSeconds() const { return _ns; }
|
||||||
constexpr ALWAYS_INLINE s64 GetMicroSeconds() const { return this->GetNanoSeconds() / (INT64_C(1000)); }
|
constexpr ALWAYS_INLINE s64 GetMicroSeconds() const { return this->GetNanoSeconds() / (INT64_C(1000)); }
|
||||||
constexpr ALWAYS_INLINE s64 GetMilliSeconds() const { return this->GetNanoSeconds() / (INT64_C(1000) * INT64_C(1000)); }
|
constexpr ALWAYS_INLINE s64 GetMilliSeconds() const { return this->GetNanoSeconds() / (INT64_C(1000) * INT64_C(1000)); }
|
||||||
constexpr ALWAYS_INLINE s64 GetSeconds() const { return this->GetNanoSeconds() / (INT64_C(1000) * INT64_C(1000) * INT64_C(1000)); }
|
constexpr ALWAYS_INLINE s64 GetSeconds() const { return this->GetNanoSeconds() / (INT64_C(1000) * INT64_C(1000) * INT64_C(1000)); }
|
||||||
|
@ -42,15 +42,15 @@ namespace ams {
|
||||||
constexpr ALWAYS_INLINE s64 GetHours() const { return this->GetNanoSeconds() / (INT64_C(1000) * INT64_C(1000) * INT64_C(1000) * INT64_C( 60) * INT64_C( 60)); }
|
constexpr ALWAYS_INLINE s64 GetHours() const { return this->GetNanoSeconds() / (INT64_C(1000) * INT64_C(1000) * INT64_C(1000) * INT64_C( 60) * INT64_C( 60)); }
|
||||||
constexpr ALWAYS_INLINE s64 GetDays() const { return this->GetNanoSeconds() / (INT64_C(1000) * INT64_C(1000) * INT64_C(1000) * INT64_C( 60) * INT64_C( 60) * INT64_C( 24)); }
|
constexpr ALWAYS_INLINE s64 GetDays() const { return this->GetNanoSeconds() / (INT64_C(1000) * INT64_C(1000) * INT64_C(1000) * INT64_C( 60) * INT64_C( 60) * INT64_C( 24)); }
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE friend bool operator==(const TimeSpanType &lhs, const TimeSpanType &rhs) { return lhs.ns == rhs.ns; }
|
constexpr ALWAYS_INLINE friend bool operator==(const TimeSpanType &lhs, const TimeSpanType &rhs) { return lhs._ns == rhs._ns; }
|
||||||
constexpr ALWAYS_INLINE friend bool operator!=(const TimeSpanType &lhs, const TimeSpanType &rhs) { return lhs.ns != rhs.ns; }
|
constexpr ALWAYS_INLINE friend bool operator!=(const TimeSpanType &lhs, const TimeSpanType &rhs) { return lhs._ns != rhs._ns; }
|
||||||
constexpr ALWAYS_INLINE friend bool operator<=(const TimeSpanType &lhs, const TimeSpanType &rhs) { return lhs.ns <= rhs.ns; }
|
constexpr ALWAYS_INLINE friend bool operator<=(const TimeSpanType &lhs, const TimeSpanType &rhs) { return lhs._ns <= rhs._ns; }
|
||||||
constexpr ALWAYS_INLINE friend bool operator>=(const TimeSpanType &lhs, const TimeSpanType &rhs) { return lhs.ns >= rhs.ns; }
|
constexpr ALWAYS_INLINE friend bool operator>=(const TimeSpanType &lhs, const TimeSpanType &rhs) { return lhs._ns >= rhs._ns; }
|
||||||
constexpr ALWAYS_INLINE friend bool operator< (const TimeSpanType &lhs, const TimeSpanType &rhs) { return lhs.ns < rhs.ns; }
|
constexpr ALWAYS_INLINE friend bool operator< (const TimeSpanType &lhs, const TimeSpanType &rhs) { return lhs._ns < rhs._ns; }
|
||||||
constexpr ALWAYS_INLINE friend bool operator> (const TimeSpanType &lhs, const TimeSpanType &rhs) { return lhs.ns > rhs.ns; }
|
constexpr ALWAYS_INLINE friend bool operator> (const TimeSpanType &lhs, const TimeSpanType &rhs) { return lhs._ns > rhs._ns; }
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE TimeSpanType &operator+=(const TimeSpanType &rhs) { this->ns += rhs.ns; return *this; }
|
constexpr ALWAYS_INLINE TimeSpanType &operator+=(const TimeSpanType &rhs) { _ns += rhs._ns; return *this; }
|
||||||
constexpr ALWAYS_INLINE TimeSpanType &operator-=(const TimeSpanType &rhs) { this->ns -= rhs.ns; return *this; }
|
constexpr ALWAYS_INLINE TimeSpanType &operator-=(const TimeSpanType &rhs) { _ns -= rhs._ns; return *this; }
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE friend TimeSpanType operator+(const TimeSpanType &lhs, const TimeSpanType &rhs) { TimeSpanType r(lhs); return r += rhs; }
|
constexpr ALWAYS_INLINE friend TimeSpanType operator+(const TimeSpanType &lhs, const TimeSpanType &rhs) { TimeSpanType r(lhs); return r += rhs; }
|
||||||
constexpr ALWAYS_INLINE friend TimeSpanType operator-(const TimeSpanType &lhs, const TimeSpanType &rhs) { TimeSpanType r(lhs); return r -= rhs; }
|
constexpr ALWAYS_INLINE friend TimeSpanType operator-(const TimeSpanType &lhs, const TimeSpanType &rhs) { TimeSpanType r(lhs); return r -= rhs; }
|
||||||
|
@ -61,13 +61,13 @@ namespace ams {
|
||||||
private:
|
private:
|
||||||
using ZeroTag = const class ZeroTagImpl{} *;
|
using ZeroTag = const class ZeroTagImpl{} *;
|
||||||
private:
|
private:
|
||||||
TimeSpanType ts;
|
TimeSpanType m_ts;
|
||||||
public:
|
public:
|
||||||
constexpr ALWAYS_INLINE TimeSpan(ZeroTag z = nullptr) : ts(TimeSpanType::FromNanoSeconds(0)) { AMS_UNUSED(z); /* ... */ }
|
constexpr ALWAYS_INLINE TimeSpan(ZeroTag z = nullptr) : m_ts(TimeSpanType::FromNanoSeconds(0)) { AMS_UNUSED(z); /* ... */ }
|
||||||
constexpr ALWAYS_INLINE TimeSpan(const TimeSpanType &t) : ts(t) { /* ... */ }
|
constexpr ALWAYS_INLINE TimeSpan(const TimeSpanType &t) : m_ts(t) { /* ... */ }
|
||||||
|
|
||||||
template<typename R, typename P>
|
template<typename R, typename P>
|
||||||
constexpr ALWAYS_INLINE TimeSpan(const std::chrono::duration<R, P>& c) : ts(TimeSpanType::FromNanoSeconds(static_cast<std::chrono::nanoseconds>(c).count())) { /* ... */ }
|
constexpr ALWAYS_INLINE TimeSpan(const std::chrono::duration<R, P>& c) : m_ts(TimeSpanType::FromNanoSeconds(static_cast<std::chrono::nanoseconds>(c).count())) { /* ... */ }
|
||||||
public:
|
public:
|
||||||
static constexpr ALWAYS_INLINE TimeSpan FromNanoSeconds(s64 ns) { return TimeSpanType::FromNanoSeconds(ns); }
|
static constexpr ALWAYS_INLINE TimeSpan FromNanoSeconds(s64 ns) { return TimeSpanType::FromNanoSeconds(ns); }
|
||||||
static constexpr ALWAYS_INLINE TimeSpan FromMicroSeconds(s64 ms) { return TimeSpanType::FromMicroSeconds(ms); }
|
static constexpr ALWAYS_INLINE TimeSpan FromMicroSeconds(s64 ms) { return TimeSpanType::FromMicroSeconds(ms); }
|
||||||
|
@ -77,29 +77,29 @@ namespace ams {
|
||||||
static constexpr ALWAYS_INLINE TimeSpan FromHours(s64 h) { return TimeSpanType::FromHours(h); }
|
static constexpr ALWAYS_INLINE TimeSpan FromHours(s64 h) { return TimeSpanType::FromHours(h); }
|
||||||
static constexpr ALWAYS_INLINE TimeSpan FromDays(s64 d) { return TimeSpanType::FromDays(d); }
|
static constexpr ALWAYS_INLINE TimeSpan FromDays(s64 d) { return TimeSpanType::FromDays(d); }
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE s64 GetNanoSeconds() const { return this->ts.GetNanoSeconds(); }
|
constexpr ALWAYS_INLINE s64 GetNanoSeconds() const { return m_ts.GetNanoSeconds(); }
|
||||||
constexpr ALWAYS_INLINE s64 GetMicroSeconds() const { return this->ts.GetMicroSeconds(); }
|
constexpr ALWAYS_INLINE s64 GetMicroSeconds() const { return m_ts.GetMicroSeconds(); }
|
||||||
constexpr ALWAYS_INLINE s64 GetMilliSeconds() const { return this->ts.GetMilliSeconds(); }
|
constexpr ALWAYS_INLINE s64 GetMilliSeconds() const { return m_ts.GetMilliSeconds(); }
|
||||||
constexpr ALWAYS_INLINE s64 GetSeconds() const { return this->ts.GetSeconds(); }
|
constexpr ALWAYS_INLINE s64 GetSeconds() const { return m_ts.GetSeconds(); }
|
||||||
constexpr ALWAYS_INLINE s64 GetMinutes() const { return this->ts.GetMinutes(); }
|
constexpr ALWAYS_INLINE s64 GetMinutes() const { return m_ts.GetMinutes(); }
|
||||||
constexpr ALWAYS_INLINE s64 GetHours() const { return this->ts.GetHours(); }
|
constexpr ALWAYS_INLINE s64 GetHours() const { return m_ts.GetHours(); }
|
||||||
constexpr ALWAYS_INLINE s64 GetDays() const { return this->ts.GetDays(); }
|
constexpr ALWAYS_INLINE s64 GetDays() const { return m_ts.GetDays(); }
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE friend bool operator==(const TimeSpan &lhs, const TimeSpan &rhs) { return lhs.ts == rhs.ts; }
|
constexpr ALWAYS_INLINE friend bool operator==(const TimeSpan &lhs, const TimeSpan &rhs) { return lhs.m_ts == rhs.m_ts; }
|
||||||
constexpr ALWAYS_INLINE friend bool operator!=(const TimeSpan &lhs, const TimeSpan &rhs) { return lhs.ts != rhs.ts; }
|
constexpr ALWAYS_INLINE friend bool operator!=(const TimeSpan &lhs, const TimeSpan &rhs) { return lhs.m_ts != rhs.m_ts; }
|
||||||
constexpr ALWAYS_INLINE friend bool operator<=(const TimeSpan &lhs, const TimeSpan &rhs) { return lhs.ts <= rhs.ts; }
|
constexpr ALWAYS_INLINE friend bool operator<=(const TimeSpan &lhs, const TimeSpan &rhs) { return lhs.m_ts <= rhs.m_ts; }
|
||||||
constexpr ALWAYS_INLINE friend bool operator>=(const TimeSpan &lhs, const TimeSpan &rhs) { return lhs.ts >= rhs.ts; }
|
constexpr ALWAYS_INLINE friend bool operator>=(const TimeSpan &lhs, const TimeSpan &rhs) { return lhs.m_ts >= rhs.m_ts; }
|
||||||
constexpr ALWAYS_INLINE friend bool operator< (const TimeSpan &lhs, const TimeSpan &rhs) { return lhs.ts < rhs.ts; }
|
constexpr ALWAYS_INLINE friend bool operator< (const TimeSpan &lhs, const TimeSpan &rhs) { return lhs.m_ts < rhs.m_ts; }
|
||||||
constexpr ALWAYS_INLINE friend bool operator> (const TimeSpan &lhs, const TimeSpan &rhs) { return lhs.ts > rhs.ts; }
|
constexpr ALWAYS_INLINE friend bool operator> (const TimeSpan &lhs, const TimeSpan &rhs) { return lhs.m_ts > rhs.m_ts; }
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE TimeSpan &operator+=(const TimeSpan &rhs) { this->ts += rhs.ts; return *this; }
|
constexpr ALWAYS_INLINE TimeSpan &operator+=(const TimeSpan &rhs) { m_ts += rhs.m_ts; return *this; }
|
||||||
constexpr ALWAYS_INLINE TimeSpan &operator-=(const TimeSpan &rhs) { this->ts -= rhs.ts; return *this; }
|
constexpr ALWAYS_INLINE TimeSpan &operator-=(const TimeSpan &rhs) { m_ts -= rhs.m_ts; return *this; }
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE friend TimeSpan operator+(const TimeSpan &lhs, const TimeSpan &rhs) { TimeSpan r(lhs); return r += rhs; }
|
constexpr ALWAYS_INLINE friend TimeSpan operator+(const TimeSpan &lhs, const TimeSpan &rhs) { TimeSpan r(lhs); return r += rhs; }
|
||||||
constexpr ALWAYS_INLINE friend TimeSpan operator-(const TimeSpan &lhs, const TimeSpan &rhs) { TimeSpan r(lhs); return r -= rhs; }
|
constexpr ALWAYS_INLINE friend TimeSpan operator-(const TimeSpan &lhs, const TimeSpan &rhs) { TimeSpan r(lhs); return r -= rhs; }
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE operator TimeSpanType() const {
|
constexpr ALWAYS_INLINE operator TimeSpanType() const {
|
||||||
return this->ts;
|
return m_ts;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -27,9 +27,9 @@ namespace ams::util {
|
||||||
static constexpr size_t AlignedSize = ((Size + Alignment - 1) / Alignment) * Alignment;
|
static constexpr size_t AlignedSize = ((Size + Alignment - 1) / Alignment) * Alignment;
|
||||||
static_assert(AlignedSize % Alignment == 0);
|
static_assert(AlignedSize % Alignment == 0);
|
||||||
private:
|
private:
|
||||||
u8 buffer[Alignment + AlignedSize];
|
u8 m_buffer[Alignment + AlignedSize];
|
||||||
public:
|
public:
|
||||||
ALWAYS_INLINE operator u8 *() { return reinterpret_cast<u8 *>(util::AlignUp(reinterpret_cast<uintptr_t>(this->buffer), Alignment)); }
|
ALWAYS_INLINE operator u8 *() { return reinterpret_cast<u8 *>(util::AlignUp(reinterpret_cast<uintptr_t>(m_buffer), Alignment)); }
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
|
@ -111,17 +111,17 @@ namespace ams::util {
|
||||||
class Reference {
|
class Reference {
|
||||||
friend struct BitFlagSet<N, T>;
|
friend struct BitFlagSet<N, T>;
|
||||||
private:
|
private:
|
||||||
BitFlagSet<N, T> *set;
|
BitFlagSet<N, T> *m_set;
|
||||||
s32 idx;
|
s32 m_idx;
|
||||||
private:
|
private:
|
||||||
constexpr ALWAYS_INLINE Reference() : set(nullptr), idx(0) { /* ... */ }
|
constexpr ALWAYS_INLINE Reference() : m_set(nullptr), m_idx(0) { /* ... */ }
|
||||||
constexpr ALWAYS_INLINE Reference(BitFlagSet<N, T> &s, s32 i) : set(std::addressof(s)), idx(i) { /* ... */ }
|
constexpr ALWAYS_INLINE Reference(BitFlagSet<N, T> &s, s32 i) : m_set(std::addressof(s)), m_idx(i) { /* ... */ }
|
||||||
public:
|
public:
|
||||||
constexpr ALWAYS_INLINE Reference &operator=(bool en) { this->set->Set(this->idx, en); return *this; }
|
constexpr ALWAYS_INLINE Reference &operator=(bool en) { m_set->Set(m_idx, en); return *this; }
|
||||||
constexpr ALWAYS_INLINE Reference &operator=(const Reference &r) { this->set->Set(this->idx, r); return *this; }
|
constexpr ALWAYS_INLINE Reference &operator=(const Reference &r) { m_set->Set(m_idx, r); return *this; }
|
||||||
constexpr ALWAYS_INLINE Reference &Negate() { this->set->Negate(this->idx); return *this; }
|
constexpr ALWAYS_INLINE Reference &Negate() { m_set->Negate(m_idx); return *this; }
|
||||||
constexpr ALWAYS_INLINE operator bool() const { return this->set->Test(this->idx); }
|
constexpr ALWAYS_INLINE operator bool() const { return m_set->Test(m_idx); }
|
||||||
constexpr ALWAYS_INLINE bool operator~() const { return !this->set->Test(this->idx); }
|
constexpr ALWAYS_INLINE bool operator~() const { return !m_set->Test(m_idx); }
|
||||||
};
|
};
|
||||||
|
|
||||||
template<s32 _Index>
|
template<s32 _Index>
|
||||||
|
|
|
@ -40,22 +40,22 @@ namespace ams::util {
|
||||||
return Storage(1) << (FlagsPerWord - 1 - bit);
|
return Storage(1) << (FlagsPerWord - 1 - bit);
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
Storage words[NumWords];
|
Storage m_words[NumWords];
|
||||||
public:
|
public:
|
||||||
constexpr ALWAYS_INLINE BitSet() : words() { /* ... */ }
|
constexpr ALWAYS_INLINE BitSet() : m_words() { /* ... */ }
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE void SetBit(size_t i) {
|
constexpr ALWAYS_INLINE void SetBit(size_t i) {
|
||||||
this->words[i / FlagsPerWord] |= GetBitMask(i % FlagsPerWord);
|
m_words[i / FlagsPerWord] |= GetBitMask(i % FlagsPerWord);
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE void ClearBit(size_t i) {
|
constexpr ALWAYS_INLINE void ClearBit(size_t i) {
|
||||||
this->words[i / FlagsPerWord] &= ~GetBitMask(i % FlagsPerWord);
|
m_words[i / FlagsPerWord] &= ~GetBitMask(i % FlagsPerWord);
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE size_t CountLeadingZero() const {
|
constexpr ALWAYS_INLINE size_t CountLeadingZero() const {
|
||||||
for (size_t i = 0; i < NumWords; i++) {
|
for (size_t i = 0; i < NumWords; i++) {
|
||||||
if (this->words[i]) {
|
if (m_words[i]) {
|
||||||
return FlagsPerWord * i + CountLeadingZeroImpl(this->words[i]);
|
return FlagsPerWord * i + CountLeadingZeroImpl(m_words[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return FlagsPerWord * NumWords;
|
return FlagsPerWord * NumWords;
|
||||||
|
@ -63,7 +63,7 @@ namespace ams::util {
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE size_t GetNextSet(size_t n) const {
|
constexpr ALWAYS_INLINE size_t GetNextSet(size_t n) const {
|
||||||
for (size_t i = (n + 1) / FlagsPerWord; i < NumWords; i++) {
|
for (size_t i = (n + 1) / FlagsPerWord; i < NumWords; i++) {
|
||||||
Storage word = this->words[i];
|
Storage word = m_words[i];
|
||||||
if (!util::IsAligned(n + 1, FlagsPerWord)) {
|
if (!util::IsAligned(n + 1, FlagsPerWord)) {
|
||||||
word &= GetBitMask(n % FlagsPerWord) - 1;
|
word &= GetBitMask(n % FlagsPerWord) - 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,32 +37,32 @@ namespace ams::util {
|
||||||
return __builtin_ctzll(static_cast<u64>(v));
|
return __builtin_ctzll(static_cast<u64>(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
T value;
|
T m_value;
|
||||||
public:
|
public:
|
||||||
/* Note: GCC has a bug in constant-folding here. Workaround: wrap entire caller with constexpr. */
|
/* Note: GCC has a bug in constant-folding here. Workaround: wrap entire caller with constexpr. */
|
||||||
constexpr ALWAYS_INLINE BitsOf(T value = T(0u)) : value(value) {
|
constexpr ALWAYS_INLINE BitsOf(T value = T(0u)) : m_value(value) {
|
||||||
/* ... */
|
/* ... */
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE bool operator==(const BitsOf &other) const {
|
constexpr ALWAYS_INLINE bool operator==(const BitsOf &other) const {
|
||||||
return this->value == other.value;
|
return m_value == other.m_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE bool operator!=(const BitsOf &other) const {
|
constexpr ALWAYS_INLINE bool operator!=(const BitsOf &other) const {
|
||||||
return this->value != other.value;
|
return m_value != other.m_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE int operator*() const {
|
constexpr ALWAYS_INLINE int operator*() const {
|
||||||
return GetLsbPos(this->value);
|
return GetLsbPos(m_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE BitsOf &operator++() {
|
constexpr ALWAYS_INLINE BitsOf &operator++() {
|
||||||
this->value &= ~(T(1u) << GetLsbPos(this->value));
|
m_value &= ~(T(1u) << GetLsbPos(m_value));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE BitsOf &operator++(int) {
|
constexpr ALWAYS_INLINE BitsOf &operator++(int) {
|
||||||
BitsOf ret(this->value);
|
BitsOf ret(m_value);
|
||||||
++(*this);
|
++(*this);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,20 +24,20 @@ namespace ams::util {
|
||||||
template<class Key, class Value, size_t N>
|
template<class Key, class Value, size_t N>
|
||||||
class BoundedMap {
|
class BoundedMap {
|
||||||
private:
|
private:
|
||||||
std::array<util::optional<Key>, N> keys;
|
std::array<util::optional<Key>, N> m_keys;
|
||||||
std::array<TypedStorage<Value>, N> values;
|
std::array<TypedStorage<Value>, N> m_values;
|
||||||
private:
|
private:
|
||||||
ALWAYS_INLINE void FreeEntry(size_t i) {
|
ALWAYS_INLINE void FreeEntry(size_t i) {
|
||||||
this->keys[i].reset();
|
m_keys[i].reset();
|
||||||
DestroyAt(this->values[i]);
|
DestroyAt(m_values[i]);
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
constexpr BoundedMap() : keys(), values() { /* ... */ }
|
constexpr BoundedMap() : m_keys(), m_values() { /* ... */ }
|
||||||
|
|
||||||
Value *Find(const Key &key) {
|
Value *Find(const Key &key) {
|
||||||
for (size_t i = 0; i < N; i++) {
|
for (size_t i = 0; i < N; i++) {
|
||||||
if (this->keys[i] && this->keys[i].value() == key) {
|
if (m_keys[i] && m_keys[i].value() == key) {
|
||||||
return GetPointer(this->values[i]);
|
return GetPointer(m_values[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -45,7 +45,7 @@ namespace ams::util {
|
||||||
|
|
||||||
void Remove(const Key &key) {
|
void Remove(const Key &key) {
|
||||||
for (size_t i = 0; i < N; i++) {
|
for (size_t i = 0; i < N; i++) {
|
||||||
if (this->keys[i] && this->keys[i].value() == key) {
|
if (m_keys[i] && m_keys[i].value() == key) {
|
||||||
this->FreeEntry(i);
|
this->FreeEntry(i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -60,7 +60,7 @@ namespace ams::util {
|
||||||
|
|
||||||
bool IsFull() {
|
bool IsFull() {
|
||||||
for (size_t i = 0; i < N; i++) {
|
for (size_t i = 0; i < N; i++) {
|
||||||
if (!this->keys[i]) {
|
if (!m_keys[i]) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -76,9 +76,9 @@ namespace ams::util {
|
||||||
|
|
||||||
/* Find a free value. */
|
/* Find a free value. */
|
||||||
for (size_t i = 0; i < N; i++) {
|
for (size_t i = 0; i < N; i++) {
|
||||||
if (!this->keys[i]) {
|
if (!m_keys[i]) {
|
||||||
this->keys[i] = key;
|
m_keys[i] = key;
|
||||||
ConstructAt(this->values[i], std::forward<Value>(value));
|
ConstructAt(m_values[i], std::forward<Value>(value));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,17 +89,17 @@ namespace ams::util {
|
||||||
bool InsertOrAssign(const Key &key, Value &&value) {
|
bool InsertOrAssign(const Key &key, Value &&value) {
|
||||||
/* Try to find and assign an existing value. */
|
/* Try to find and assign an existing value. */
|
||||||
for (size_t i = 0; i < N; i++) {
|
for (size_t i = 0; i < N; i++) {
|
||||||
if (this->keys[i] && this->keys[i].value() == key) {
|
if (m_keys[i] && m_keys[i].value() == key) {
|
||||||
GetReference(this->values[i]) = std::forward<Value>(value);
|
GetReference(m_values[i]) = std::forward<Value>(value);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Find a free value. */
|
/* Find a free value. */
|
||||||
for (size_t i = 0; i < N; i++) {
|
for (size_t i = 0; i < N; i++) {
|
||||||
if (!this->keys[i]) {
|
if (!m_keys[i]) {
|
||||||
this->keys[i] = key;
|
m_keys[i] = key;
|
||||||
ConstructAt(this->values[i], std::move(value));
|
ConstructAt(m_values[i], std::move(value));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -116,9 +116,9 @@ namespace ams::util {
|
||||||
|
|
||||||
/* Find a free value. */
|
/* Find a free value. */
|
||||||
for (size_t i = 0; i < N; i++) {
|
for (size_t i = 0; i < N; i++) {
|
||||||
if (!this->keys[i]) {
|
if (!m_keys[i]) {
|
||||||
this->keys[i] = key;
|
m_keys[i] = key;
|
||||||
ConstructAt(this->values[i], std::forward<Args>(args)...);
|
ConstructAt(m_values[i], std::forward<Args>(args)...);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -119,22 +119,22 @@ namespace ams::util {
|
||||||
private:
|
private:
|
||||||
friend class ConstIterator;
|
friend class ConstIterator;
|
||||||
private:
|
private:
|
||||||
const FixedTree *m_this;
|
const FixedTree *m_tree;
|
||||||
int m_index;
|
int m_index;
|
||||||
protected:
|
protected:
|
||||||
constexpr ALWAYS_INLINE IteratorBase(const FixedTree *tree, int index) : m_this(tree), m_index(index) { /* ... */ }
|
constexpr ALWAYS_INLINE IteratorBase(const FixedTree *tree, int index) : m_tree(tree), m_index(index) { /* ... */ }
|
||||||
|
|
||||||
constexpr bool IsEqualImpl(const IteratorBase &rhs) const {
|
constexpr bool IsEqualImpl(const IteratorBase &rhs) const {
|
||||||
/* Validate pre-conditions. */
|
/* Validate pre-conditions. */
|
||||||
AMS_ASSERT(m_this);
|
AMS_ASSERT(m_tree);
|
||||||
|
|
||||||
/* Check for tree equality. */
|
/* Check for tree equality. */
|
||||||
if (m_this != rhs.m_this) {
|
if (m_tree != rhs.m_tree) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check for nil. */
|
/* Check for nil. */
|
||||||
if (m_this->IsNil(m_index) && m_this->IsNil(rhs.m_index)) {
|
if (m_tree->IsNil(m_index) && m_tree->IsNil(rhs.m_index)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,19 +144,19 @@ namespace ams::util {
|
||||||
|
|
||||||
constexpr IteratorMember &DereferenceImpl() const {
|
constexpr IteratorMember &DereferenceImpl() const {
|
||||||
/* Validate pre-conditions. */
|
/* Validate pre-conditions. */
|
||||||
AMS_ASSERT(m_this);
|
AMS_ASSERT(m_tree);
|
||||||
|
|
||||||
if (!m_this->IsNil(m_index)) {
|
if (!m_tree->IsNil(m_index)) {
|
||||||
return m_this->m_nodes[m_index].m_data;
|
return m_tree->m_nodes[m_index].m_data;
|
||||||
} else {
|
} else {
|
||||||
AMS_ASSERT(false);
|
AMS_ASSERT(false);
|
||||||
return m_this->GetNode(std::numeric_limits<int>::max())->m_data;
|
return m_tree->GetNode(std::numeric_limits<int>::max())->m_data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE IteratorBase &IncrementImpl() {
|
constexpr ALWAYS_INLINE IteratorBase &IncrementImpl() {
|
||||||
/* Validate pre-conditions. */
|
/* Validate pre-conditions. */
|
||||||
AMS_ASSERT(m_this);
|
AMS_ASSERT(m_tree);
|
||||||
|
|
||||||
this->OperateIndex(true);
|
this->OperateIndex(true);
|
||||||
return *this;
|
return *this;
|
||||||
|
@ -164,7 +164,7 @@ namespace ams::util {
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE IteratorBase &DecrementImpl() {
|
constexpr ALWAYS_INLINE IteratorBase &DecrementImpl() {
|
||||||
/* Validate pre-conditions. */
|
/* Validate pre-conditions. */
|
||||||
AMS_ASSERT(m_this);
|
AMS_ASSERT(m_tree);
|
||||||
|
|
||||||
this->OperateIndex(false);
|
this->OperateIndex(false);
|
||||||
return *this;
|
return *this;
|
||||||
|
@ -176,18 +176,18 @@ namespace ams::util {
|
||||||
if (m_index == Index_BeforeBegin) {
|
if (m_index == Index_BeforeBegin) {
|
||||||
m_index = 0;
|
m_index = 0;
|
||||||
} else {
|
} else {
|
||||||
m_index = m_this->UncheckedPP(m_index);
|
m_index = m_tree->UncheckedPP(m_index);
|
||||||
if (m_this->IsNil(m_index)) {
|
if (m_tree->IsNil(m_index)) {
|
||||||
m_index = Index_AfterEnd;
|
m_index = Index_AfterEnd;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/* We're decrementing. */
|
/* We're decrementing. */
|
||||||
if (m_index == Index_AfterEnd) {
|
if (m_index == Index_AfterEnd) {
|
||||||
m_index = static_cast<int>(m_this->size()) - 1;
|
m_index = static_cast<int>(m_tree->size()) - 1;
|
||||||
} else {
|
} else {
|
||||||
m_index = m_this->UncheckedMM(m_index);
|
m_index = m_tree->UncheckedMM(m_index);
|
||||||
if (m_this->IsNil(m_index)) {
|
if (m_tree->IsNil(m_index)) {
|
||||||
m_index = Index_BeforeBegin;
|
m_index = Index_BeforeBegin;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -233,7 +233,7 @@ namespace ams::util {
|
||||||
constexpr ALWAYS_INLINE ConstIterator(const FixedTree &tree, int index) : IteratorBase(std::addressof(tree), index) { /* ... */ }
|
constexpr ALWAYS_INLINE ConstIterator(const FixedTree &tree, int index) : IteratorBase(std::addressof(tree), index) { /* ... */ }
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE ConstIterator(const ConstIterator &rhs) = default;
|
constexpr ALWAYS_INLINE ConstIterator(const ConstIterator &rhs) = default;
|
||||||
constexpr ALWAYS_INLINE ConstIterator(const Iterator &rhs) : IteratorBase(rhs.m_this, rhs.m_index) { /* ... */ }
|
constexpr ALWAYS_INLINE ConstIterator(const Iterator &rhs) : IteratorBase(rhs.m_tree, rhs.m_index) { /* ... */ }
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE bool operator==(const ConstIterator &rhs) const {
|
constexpr ALWAYS_INLINE bool operator==(const ConstIterator &rhs) const {
|
||||||
return this->IsEqualImpl(rhs);
|
return this->IsEqualImpl(rhs);
|
||||||
|
|
|
@ -36,13 +36,13 @@ namespace ams::util {
|
||||||
private:
|
private:
|
||||||
friend class impl::IntrusiveListImpl;
|
friend class impl::IntrusiveListImpl;
|
||||||
|
|
||||||
IntrusiveListNode *prev;
|
IntrusiveListNode *m_prev;
|
||||||
IntrusiveListNode *next;
|
IntrusiveListNode *m_next;
|
||||||
public:
|
public:
|
||||||
constexpr ALWAYS_INLINE IntrusiveListNode() : prev(this), next(this) { /* ... */ }
|
constexpr ALWAYS_INLINE IntrusiveListNode() : m_prev(this), m_next(this) { /* ... */ }
|
||||||
|
|
||||||
constexpr ALWAYS_INLINE bool IsLinked() const {
|
constexpr ALWAYS_INLINE bool IsLinked() const {
|
||||||
return this->next != this;
|
return m_next != this;
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
ALWAYS_INLINE void LinkPrev(IntrusiveListNode *node) {
|
ALWAYS_INLINE void LinkPrev(IntrusiveListNode *node) {
|
||||||
|
@ -53,11 +53,11 @@ namespace ams::util {
|
||||||
|
|
||||||
ALWAYS_INLINE void SplicePrev(IntrusiveListNode *first, IntrusiveListNode *last) {
|
ALWAYS_INLINE void SplicePrev(IntrusiveListNode *first, IntrusiveListNode *last) {
|
||||||
/* Splice a range into the list. */
|
/* Splice a range into the list. */
|
||||||
auto last_prev = last->prev;
|
auto last_prev = last->m_prev;
|
||||||
first->prev = this->prev;
|
first->m_prev = m_prev;
|
||||||
this->prev->next = first;
|
last_prev->m_next = this;
|
||||||
last_prev->next = this;
|
m_prev->m_next = first;
|
||||||
this->prev = last_prev;
|
m_prev = last_prev;
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE void LinkNext(IntrusiveListNode *node) {
|
ALWAYS_INLINE void LinkNext(IntrusiveListNode *node) {
|
||||||
|
@ -68,40 +68,40 @@ namespace ams::util {
|
||||||
|
|
||||||
ALWAYS_INLINE void SpliceNext(IntrusiveListNode *first, IntrusiveListNode *last) {
|
ALWAYS_INLINE void SpliceNext(IntrusiveListNode *first, IntrusiveListNode *last) {
|
||||||
/* Splice a range into the list. */
|
/* Splice a range into the list. */
|
||||||
auto last_prev = last->prev;
|
auto last_prev = last->m_prev;
|
||||||
first->prev = this;
|
first->m_prev = this;
|
||||||
last_prev->next = next;
|
last_prev->m_next = m_next;
|
||||||
this->next->prev = last_prev;
|
m_next->m_prev = last_prev;
|
||||||
this->next = first;
|
m_next = first;
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE void Unlink() {
|
ALWAYS_INLINE void Unlink() {
|
||||||
this->Unlink(this->next);
|
this->Unlink(m_next);
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE void Unlink(IntrusiveListNode *last) {
|
ALWAYS_INLINE void Unlink(IntrusiveListNode *last) {
|
||||||
/* Unlink a node from a next node. */
|
/* Unlink a node from a next node. */
|
||||||
auto last_prev = last->prev;
|
auto last_prev = last->m_prev;
|
||||||
this->prev->next = last;
|
m_prev->m_next = last;
|
||||||
last->prev = this->prev;
|
last->m_prev = m_prev;
|
||||||
last_prev->next = this;
|
last_prev->m_next = this;
|
||||||
this->prev = last_prev;
|
m_prev = last_prev;
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE IntrusiveListNode *GetPrev() {
|
ALWAYS_INLINE IntrusiveListNode *GetPrev() {
|
||||||
return this->prev;
|
return m_prev;
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE const IntrusiveListNode *GetPrev() const {
|
ALWAYS_INLINE const IntrusiveListNode *GetPrev() const {
|
||||||
return this->prev;
|
return m_prev;
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE IntrusiveListNode *GetNext() {
|
ALWAYS_INLINE IntrusiveListNode *GetNext() {
|
||||||
return this->next;
|
return m_next;
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE const IntrusiveListNode *GetNext() const {
|
ALWAYS_INLINE const IntrusiveListNode *GetNext() const {
|
||||||
return this->next;
|
return m_next;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
/* DEPRECATED: static_assert(std::is_literal_type<IntrusiveListNode>::value); */
|
/* DEPRECATED: static_assert(std::is_literal_type<IntrusiveListNode>::value); */
|
||||||
|
@ -111,7 +111,7 @@ namespace ams::util {
|
||||||
class IntrusiveListImpl {
|
class IntrusiveListImpl {
|
||||||
NON_COPYABLE(IntrusiveListImpl);
|
NON_COPYABLE(IntrusiveListImpl);
|
||||||
private:
|
private:
|
||||||
IntrusiveListNode root_node;
|
IntrusiveListNode m_root_node;
|
||||||
public:
|
public:
|
||||||
template<bool Const>
|
template<bool Const>
|
||||||
class Iterator;
|
class Iterator;
|
||||||
|
@ -137,12 +137,12 @@ namespace ams::util {
|
||||||
using pointer = typename std::conditional<Const, IntrusiveListImpl::const_pointer, IntrusiveListImpl::pointer>::type;
|
using pointer = typename std::conditional<Const, IntrusiveListImpl::const_pointer, IntrusiveListImpl::pointer>::type;
|
||||||
using reference = typename std::conditional<Const, IntrusiveListImpl::const_reference, IntrusiveListImpl::reference>::type;
|
using reference = typename std::conditional<Const, IntrusiveListImpl::const_reference, IntrusiveListImpl::reference>::type;
|
||||||
private:
|
private:
|
||||||
pointer node;
|
pointer m_node;
|
||||||
public:
|
public:
|
||||||
ALWAYS_INLINE explicit Iterator(pointer n) : node(n) { /* ... */ }
|
ALWAYS_INLINE explicit Iterator(pointer n) : m_node(n) { /* ... */ }
|
||||||
|
|
||||||
ALWAYS_INLINE bool operator==(const Iterator &rhs) const {
|
ALWAYS_INLINE bool operator==(const Iterator &rhs) const {
|
||||||
return this->node == rhs.node;
|
return m_node == rhs.m_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE bool operator!=(const Iterator &rhs) const {
|
ALWAYS_INLINE bool operator!=(const Iterator &rhs) const {
|
||||||
|
@ -150,20 +150,20 @@ namespace ams::util {
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE pointer operator->() const {
|
ALWAYS_INLINE pointer operator->() const {
|
||||||
return this->node;
|
return m_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE reference operator*() const {
|
ALWAYS_INLINE reference operator*() const {
|
||||||
return *this->node;
|
return *m_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE Iterator &operator++() {
|
ALWAYS_INLINE Iterator &operator++() {
|
||||||
this->node = this->node->next;
|
m_node = m_node->m_next;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE Iterator &operator--() {
|
ALWAYS_INLINE Iterator &operator--() {
|
||||||
this->node = this->node->prev;
|
m_node = m_node->m_prev;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -180,31 +180,31 @@ namespace ams::util {
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE operator Iterator<true>() const {
|
ALWAYS_INLINE operator Iterator<true>() const {
|
||||||
return Iterator<true>(this->node);
|
return Iterator<true>(m_node);
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE Iterator<false> GetNonConstIterator() const {
|
ALWAYS_INLINE Iterator<false> GetNonConstIterator() const {
|
||||||
return Iterator<false>(const_cast<IntrusiveListImpl::pointer>(this->node));
|
return Iterator<false>(const_cast<IntrusiveListImpl::pointer>(m_node));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
public:
|
public:
|
||||||
constexpr ALWAYS_INLINE IntrusiveListImpl() : root_node() { /* ... */ }
|
constexpr ALWAYS_INLINE IntrusiveListImpl() : m_root_node() { /* ... */ }
|
||||||
|
|
||||||
/* Iterator accessors. */
|
/* Iterator accessors. */
|
||||||
ALWAYS_INLINE iterator begin() {
|
ALWAYS_INLINE iterator begin() {
|
||||||
return iterator(this->root_node.GetNext());
|
return iterator(m_root_node.GetNext());
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE const_iterator begin() const {
|
ALWAYS_INLINE const_iterator begin() const {
|
||||||
return const_iterator(this->root_node.GetNext());
|
return const_iterator(m_root_node.GetNext());
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE iterator end() {
|
ALWAYS_INLINE iterator end() {
|
||||||
return iterator(std::addressof(this->root_node));
|
return iterator(std::addressof(m_root_node));
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE const_iterator end() const {
|
ALWAYS_INLINE const_iterator end() const {
|
||||||
return const_iterator(std::addressof(this->root_node));
|
return const_iterator(std::addressof(m_root_node));
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE iterator iterator_to(reference v) {
|
ALWAYS_INLINE iterator iterator_to(reference v) {
|
||||||
|
@ -221,7 +221,7 @@ namespace ams::util {
|
||||||
|
|
||||||
/* Content management. */
|
/* Content management. */
|
||||||
ALWAYS_INLINE bool empty() const {
|
ALWAYS_INLINE bool empty() const {
|
||||||
return !this->root_node.IsLinked();
|
return !m_root_node.IsLinked();
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE size_type size() const {
|
ALWAYS_INLINE size_type size() const {
|
||||||
|
@ -229,35 +229,35 @@ namespace ams::util {
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE reference back() {
|
ALWAYS_INLINE reference back() {
|
||||||
return *this->root_node.GetPrev();
|
return *m_root_node.GetPrev();
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE const_reference back() const {
|
ALWAYS_INLINE const_reference back() const {
|
||||||
return *this->root_node.GetPrev();
|
return *m_root_node.GetPrev();
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE reference front() {
|
ALWAYS_INLINE reference front() {
|
||||||
return *this->root_node.GetNext();
|
return *m_root_node.GetNext();
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE const_reference front() const {
|
ALWAYS_INLINE const_reference front() const {
|
||||||
return *this->root_node.GetNext();
|
return *m_root_node.GetNext();
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE void push_back(reference node) {
|
ALWAYS_INLINE void push_back(reference node) {
|
||||||
this->root_node.LinkPrev(std::addressof(node));
|
m_root_node.LinkPrev(std::addressof(node));
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE void push_front(reference node) {
|
ALWAYS_INLINE void push_front(reference node) {
|
||||||
this->root_node.LinkNext(std::addressof(node));
|
m_root_node.LinkNext(std::addressof(node));
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE void pop_back() {
|
ALWAYS_INLINE void pop_back() {
|
||||||
this->root_node.GetPrev()->Unlink();
|
m_root_node.GetPrev()->Unlink();
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE void pop_front() {
|
ALWAYS_INLINE void pop_front() {
|
||||||
this->root_node.GetNext()->Unlink();
|
m_root_node.GetNext()->Unlink();
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE iterator insert(const_iterator pos, reference node) {
|
ALWAYS_INLINE iterator insert(const_iterator pos, reference node) {
|
||||||
|
@ -315,7 +315,7 @@ namespace ams::util {
|
||||||
class IntrusiveList {
|
class IntrusiveList {
|
||||||
NON_COPYABLE(IntrusiveList);
|
NON_COPYABLE(IntrusiveList);
|
||||||
private:
|
private:
|
||||||
impl::IntrusiveListImpl impl;
|
impl::IntrusiveListImpl m_impl;
|
||||||
public:
|
public:
|
||||||
template<bool Const>
|
template<bool Const>
|
||||||
class Iterator;
|
class Iterator;
|
||||||
|
@ -345,16 +345,16 @@ namespace ams::util {
|
||||||
using pointer = typename std::conditional<Const, IntrusiveList::const_pointer, IntrusiveList::pointer>::type;
|
using pointer = typename std::conditional<Const, IntrusiveList::const_pointer, IntrusiveList::pointer>::type;
|
||||||
using reference = typename std::conditional<Const, IntrusiveList::const_reference, IntrusiveList::reference>::type;
|
using reference = typename std::conditional<Const, IntrusiveList::const_reference, IntrusiveList::reference>::type;
|
||||||
private:
|
private:
|
||||||
ImplIterator iterator;
|
ImplIterator m_iterator;
|
||||||
private:
|
private:
|
||||||
explicit ALWAYS_INLINE Iterator(ImplIterator it) : iterator(it) { /* ... */ }
|
explicit ALWAYS_INLINE Iterator(ImplIterator it) : m_iterator(it) { /* ... */ }
|
||||||
|
|
||||||
ALWAYS_INLINE ImplIterator GetImplIterator() const {
|
ALWAYS_INLINE ImplIterator GetImplIterator() const {
|
||||||
return this->iterator;
|
return m_iterator;
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
ALWAYS_INLINE bool operator==(const Iterator &rhs) const {
|
ALWAYS_INLINE bool operator==(const Iterator &rhs) const {
|
||||||
return this->iterator == rhs.iterator;
|
return m_iterator == rhs.m_iterator;
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE bool operator!=(const Iterator &rhs) const {
|
ALWAYS_INLINE bool operator!=(const Iterator &rhs) const {
|
||||||
|
@ -362,37 +362,37 @@ namespace ams::util {
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE pointer operator->() const {
|
ALWAYS_INLINE pointer operator->() const {
|
||||||
return std::addressof(Traits::GetParent(*this->iterator));
|
return std::addressof(Traits::GetParent(*m_iterator));
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE reference operator*() const {
|
ALWAYS_INLINE reference operator*() const {
|
||||||
return Traits::GetParent(*this->iterator);
|
return Traits::GetParent(*m_iterator);
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE Iterator &operator++() {
|
ALWAYS_INLINE Iterator &operator++() {
|
||||||
++this->iterator;
|
++m_iterator;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE Iterator &operator--() {
|
ALWAYS_INLINE Iterator &operator--() {
|
||||||
--this->iterator;
|
--m_iterator;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE Iterator operator++(int) {
|
ALWAYS_INLINE Iterator operator++(int) {
|
||||||
const Iterator it{*this};
|
const Iterator it{*this};
|
||||||
++this->iterator;
|
++m_iterator;
|
||||||
return it;
|
return it;
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE Iterator operator--(int) {
|
ALWAYS_INLINE Iterator operator--(int) {
|
||||||
const Iterator it{*this};
|
const Iterator it{*this};
|
||||||
--this->iterator;
|
--m_iterator;
|
||||||
return it;
|
return it;
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE operator Iterator<true>() const {
|
ALWAYS_INLINE operator Iterator<true>() const {
|
||||||
return Iterator<true>(this->iterator);
|
return Iterator<true>(m_iterator);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
private:
|
private:
|
||||||
|
@ -412,23 +412,23 @@ namespace ams::util {
|
||||||
return Traits::GetParent(node);
|
return Traits::GetParent(node);
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
constexpr ALWAYS_INLINE IntrusiveList() : impl() { /* ... */ }
|
constexpr ALWAYS_INLINE IntrusiveList() : m_impl() { /* ... */ }
|
||||||
|
|
||||||
/* Iterator accessors. */
|
/* Iterator accessors. */
|
||||||
ALWAYS_INLINE iterator begin() {
|
ALWAYS_INLINE iterator begin() {
|
||||||
return iterator(this->impl.begin());
|
return iterator(m_impl.begin());
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE const_iterator begin() const {
|
ALWAYS_INLINE const_iterator begin() const {
|
||||||
return const_iterator(this->impl.begin());
|
return const_iterator(m_impl.begin());
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE iterator end() {
|
ALWAYS_INLINE iterator end() {
|
||||||
return iterator(this->impl.end());
|
return iterator(m_impl.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE const_iterator end() const {
|
ALWAYS_INLINE const_iterator end() const {
|
||||||
return const_iterator(this->impl.end());
|
return const_iterator(m_impl.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE const_iterator cbegin() const {
|
ALWAYS_INLINE const_iterator cbegin() const {
|
||||||
|
@ -464,82 +464,82 @@ namespace ams::util {
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE iterator iterator_to(reference v) {
|
ALWAYS_INLINE iterator iterator_to(reference v) {
|
||||||
return iterator(this->impl.iterator_to(GetNode(v)));
|
return iterator(m_impl.iterator_to(GetNode(v)));
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE const_iterator iterator_to(const_reference v) const {
|
ALWAYS_INLINE const_iterator iterator_to(const_reference v) const {
|
||||||
return const_iterator(this->impl.iterator_to(GetNode(v)));
|
return const_iterator(m_impl.iterator_to(GetNode(v)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Content management. */
|
/* Content management. */
|
||||||
ALWAYS_INLINE bool empty() const {
|
ALWAYS_INLINE bool empty() const {
|
||||||
return this->impl.empty();
|
return m_impl.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE size_type size() const {
|
ALWAYS_INLINE size_type size() const {
|
||||||
return this->impl.size();
|
return m_impl.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE reference back() {
|
ALWAYS_INLINE reference back() {
|
||||||
AMS_ASSERT(!this->impl.empty());
|
AMS_ASSERT(!m_impl.empty());
|
||||||
return GetParent(this->impl.back());
|
return GetParent(m_impl.back());
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE const_reference back() const {
|
ALWAYS_INLINE const_reference back() const {
|
||||||
AMS_ASSERT(!this->impl.empty());
|
AMS_ASSERT(!m_impl.empty());
|
||||||
return GetParent(this->impl.back());
|
return GetParent(m_impl.back());
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE reference front() {
|
ALWAYS_INLINE reference front() {
|
||||||
AMS_ASSERT(!this->impl.empty());
|
AMS_ASSERT(!m_impl.empty());
|
||||||
return GetParent(this->impl.front());
|
return GetParent(m_impl.front());
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE const_reference front() const {
|
ALWAYS_INLINE const_reference front() const {
|
||||||
AMS_ASSERT(!this->impl.empty());
|
AMS_ASSERT(!m_impl.empty());
|
||||||
return GetParent(this->impl.front());
|
return GetParent(m_impl.front());
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE void push_back(reference ref) {
|
ALWAYS_INLINE void push_back(reference ref) {
|
||||||
this->impl.push_back(GetNode(ref));
|
m_impl.push_back(GetNode(ref));
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE void push_front(reference ref) {
|
ALWAYS_INLINE void push_front(reference ref) {
|
||||||
this->impl.push_front(GetNode(ref));
|
m_impl.push_front(GetNode(ref));
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE void pop_back() {
|
ALWAYS_INLINE void pop_back() {
|
||||||
AMS_ASSERT(!this->impl.empty());
|
AMS_ASSERT(!m_impl.empty());
|
||||||
this->impl.pop_back();
|
m_impl.pop_back();
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE void pop_front() {
|
ALWAYS_INLINE void pop_front() {
|
||||||
AMS_ASSERT(!this->impl.empty());
|
AMS_ASSERT(!m_impl.empty());
|
||||||
this->impl.pop_front();
|
m_impl.pop_front();
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE iterator insert(const_iterator pos, reference ref) {
|
ALWAYS_INLINE iterator insert(const_iterator pos, reference ref) {
|
||||||
return iterator(this->impl.insert(pos.GetImplIterator(), GetNode(ref)));
|
return iterator(m_impl.insert(pos.GetImplIterator(), GetNode(ref)));
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE void splice(const_iterator pos, IntrusiveList &o) {
|
ALWAYS_INLINE void splice(const_iterator pos, IntrusiveList &o) {
|
||||||
this->impl.splice(pos.GetImplIterator(), o.impl);
|
m_impl.splice(pos.GetImplIterator(), o.m_impl);
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE void splice(const_iterator pos, IntrusiveList &o, const_iterator first) {
|
ALWAYS_INLINE void splice(const_iterator pos, IntrusiveList &o, const_iterator first) {
|
||||||
this->impl.splice(pos.GetImplIterator(), o.impl, first.GetImplIterator());
|
m_impl.splice(pos.GetImplIterator(), o.m_impl, first.GetImplIterator());
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE void splice(const_iterator pos, IntrusiveList &o, const_iterator first, const_iterator last) {
|
ALWAYS_INLINE void splice(const_iterator pos, IntrusiveList &o, const_iterator first, const_iterator last) {
|
||||||
this->impl.splice(pos.GetImplIterator(), o.impl, first.GetImplIterator(), last.GetImplIterator());
|
m_impl.splice(pos.GetImplIterator(), o.m_impl, first.GetImplIterator(), last.GetImplIterator());
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE iterator erase(const_iterator pos) {
|
ALWAYS_INLINE iterator erase(const_iterator pos) {
|
||||||
return iterator(this->impl.erase(pos.GetImplIterator()));
|
return iterator(m_impl.erase(pos.GetImplIterator()));
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE void clear() {
|
ALWAYS_INLINE void clear() {
|
||||||
this->impl.clear();
|
m_impl.clear();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -51,20 +51,20 @@ namespace ams::util {
|
||||||
return value ^ (value >> 30);
|
return value ^ (value >> 30);
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
State state;
|
State m_state;
|
||||||
private:
|
private:
|
||||||
/* Internal API. */
|
/* Internal API. */
|
||||||
void FinalizeInitialization() {
|
void FinalizeInitialization() {
|
||||||
const u32 state0 = this->state.data[0] & TopBitmask;
|
const u32 state0 = m_state.data[0] & TopBitmask;
|
||||||
const u32 state1 = this->state.data[1];
|
const u32 state1 = m_state.data[1];
|
||||||
const u32 state2 = this->state.data[2];
|
const u32 state2 = m_state.data[2];
|
||||||
const u32 state3 = this->state.data[3];
|
const u32 state3 = m_state.data[3];
|
||||||
|
|
||||||
if (state0 == 0 && state1 == 0 && state2 == 0 && state3 == 0) {
|
if (state0 == 0 && state1 == 0 && state2 == 0 && state3 == 0) {
|
||||||
this->state.data[0] = 'T';
|
m_state.data[0] = 'T';
|
||||||
this->state.data[1] = 'I';
|
m_state.data[1] = 'I';
|
||||||
this->state.data[2] = 'N';
|
m_state.data[2] = 'N';
|
||||||
this->state.data[3] = 'Y';
|
m_state.data[3] = 'Y';
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < NumDiscardedInitOutputs; i++) {
|
for (int i = 0; i < NumDiscardedInitOutputs; i++) {
|
||||||
|
@ -102,42 +102,42 @@ namespace ams::util {
|
||||||
state2 ^= y;
|
state2 ^= y;
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
constexpr TinyMT() : state() { /* ... */ }
|
constexpr TinyMT() : m_state() { /* ... */ }
|
||||||
|
|
||||||
/* Public API. */
|
/* Public API. */
|
||||||
|
|
||||||
/* Initialization. */
|
/* Initialization. */
|
||||||
void Initialize(u32 seed) {
|
void Initialize(u32 seed) {
|
||||||
this->state.data[0] = seed;
|
m_state.data[0] = seed;
|
||||||
this->state.data[1] = ParamMat1;
|
m_state.data[1] = ParamMat1;
|
||||||
this->state.data[2] = ParamMat2;
|
m_state.data[2] = ParamMat2;
|
||||||
this->state.data[3] = ParamTmat;
|
m_state.data[3] = ParamTmat;
|
||||||
|
|
||||||
for (int i = 1; i < MinimumInitIterations; i++) {
|
for (int i = 1; i < MinimumInitIterations; i++) {
|
||||||
const u32 mixed = XorByShifted30(this->state.data[(i - 1) % NumStateWords]);
|
const u32 mixed = XorByShifted30(m_state.data[(i - 1) % NumStateWords]);
|
||||||
this->state.data[i % NumStateWords] ^= mixed * ParamMult + i;
|
m_state.data[i % NumStateWords] ^= mixed * ParamMult + i;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->FinalizeInitialization();
|
this->FinalizeInitialization();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Initialize(const u32 *seed, int seed_count) {
|
void Initialize(const u32 *seed, int seed_count) {
|
||||||
this->state.data[0] = 0;
|
m_state.data[0] = 0;
|
||||||
this->state.data[1] = ParamMat1;
|
m_state.data[1] = ParamMat1;
|
||||||
this->state.data[2] = ParamMat2;
|
m_state.data[2] = ParamMat2;
|
||||||
this->state.data[3] = ParamTmat;
|
m_state.data[3] = ParamTmat;
|
||||||
|
|
||||||
{
|
{
|
||||||
const int num_init_iterations = std::max(seed_count + 1, MinimumInitIterations) - 1;
|
const int num_init_iterations = std::max(seed_count + 1, MinimumInitIterations) - 1;
|
||||||
|
|
||||||
GenerateInitialValuePlus(std::addressof(this->state), 0, seed_count);
|
GenerateInitialValuePlus(std::addressof(m_state), 0, seed_count);
|
||||||
|
|
||||||
for (int i = 0; i < num_init_iterations; i++) {
|
for (int i = 0; i < num_init_iterations; i++) {
|
||||||
GenerateInitialValuePlus(std::addressof(this->state), (i + 1) % NumStateWords, (i < seed_count) ? seed[i] : 0);
|
GenerateInitialValuePlus(std::addressof(m_state), (i + 1) % NumStateWords, (i < seed_count) ? seed[i] : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < static_cast<int>(NumStateWords); i++) {
|
for (int i = 0; i < static_cast<int>(NumStateWords); i++) {
|
||||||
GenerateInitialValueXor(std::addressof(this->state), (i + 1 + num_init_iterations) % NumStateWords);
|
GenerateInitialValueXor(std::addressof(m_state), (i + 1 + num_init_iterations) % NumStateWords);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,11 +146,11 @@ namespace ams::util {
|
||||||
|
|
||||||
/* State management. */
|
/* State management. */
|
||||||
void GetState(TinyMT::State *out) const {
|
void GetState(TinyMT::State *out) const {
|
||||||
std::memcpy(out->data, this->state.data, sizeof(this->state));
|
std::memcpy(out->data, m_state.data, sizeof(m_state));
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetState(const TinyMT::State *state) {
|
void SetState(const TinyMT::State *state) {
|
||||||
std::memcpy(this->state.data, state->data, sizeof(this->state));
|
std::memcpy(m_state.data, state->data, sizeof(m_state));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Random generation. */
|
/* Random generation. */
|
||||||
|
@ -185,13 +185,13 @@ namespace ams::util {
|
||||||
|
|
||||||
NOINLINE u32 GenerateRandomU32() {
|
NOINLINE u32 GenerateRandomU32() {
|
||||||
/* Advance state. */
|
/* Advance state. */
|
||||||
const u32 x0 = (this->state.data[0] & TopBitmask) ^ this->state.data[1] ^ this->state.data[2];
|
const u32 x0 = (m_state.data[0] & TopBitmask) ^ m_state.data[1] ^ m_state.data[2];
|
||||||
const u32 y0 = this->state.data[3];
|
const u32 y0 = m_state.data[3];
|
||||||
const u32 x1 = x0 ^ (x0 << 1);
|
const u32 x1 = x0 ^ (x0 << 1);
|
||||||
const u32 y1 = y0 ^ (y0 >> 1) ^ x1;
|
const u32 y1 = y0 ^ (y0 >> 1) ^ x1;
|
||||||
|
|
||||||
const u32 state0 = this->state.data[1];
|
const u32 state0 = m_state.data[1];
|
||||||
u32 state1 = this->state.data[2];
|
u32 state1 = m_state.data[2];
|
||||||
u32 state2 = x1 ^ (y1 << 10);
|
u32 state2 = x1 ^ (y1 << 10);
|
||||||
const u32 state3 = y1;
|
const u32 state3 = y1;
|
||||||
|
|
||||||
|
@ -200,10 +200,10 @@ namespace ams::util {
|
||||||
state2 ^= ParamMat2;
|
state2 ^= ParamMat2;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->state.data[0] = state0;
|
m_state.data[0] = state0;
|
||||||
this->state.data[1] = state1;
|
m_state.data[1] = state1;
|
||||||
this->state.data[2] = state2;
|
m_state.data[2] = state2;
|
||||||
this->state.data[3] = state3;
|
m_state.data[3] = state3;
|
||||||
|
|
||||||
/* Temper. */
|
/* Temper. */
|
||||||
const u32 t1 = state0 + (state2 >> 8);
|
const u32 t1 = state0 + (state2 >> 8);
|
||||||
|
|
|
@ -40,16 +40,16 @@ namespace ams::crypto::impl {
|
||||||
|
|
||||||
if constexpr (KeySize == 16) {
|
if constexpr (KeySize == 16) {
|
||||||
/* Aes 128. */
|
/* Aes 128. */
|
||||||
static_assert(sizeof(this->round_keys) == sizeof(::Aes128Context));
|
static_assert(sizeof(m_round_keys) == sizeof(::Aes128Context));
|
||||||
aes128ContextCreate(reinterpret_cast<Aes128Context *>(this->round_keys), key, is_encrypt);
|
aes128ContextCreate(reinterpret_cast<Aes128Context *>(m_round_keys), key, is_encrypt);
|
||||||
} else if constexpr (KeySize == 24) {
|
} else if constexpr (KeySize == 24) {
|
||||||
/* Aes 192. */
|
/* Aes 192. */
|
||||||
static_assert(sizeof(this->round_keys) == sizeof(::Aes192Context));
|
static_assert(sizeof(m_round_keys) == sizeof(::Aes192Context));
|
||||||
aes192ContextCreate(reinterpret_cast<Aes192Context *>(this->round_keys), key, is_encrypt);
|
aes192ContextCreate(reinterpret_cast<Aes192Context *>(m_round_keys), key, is_encrypt);
|
||||||
} else if constexpr (KeySize == 32) {
|
} else if constexpr (KeySize == 32) {
|
||||||
/* Aes 256. */
|
/* Aes 256. */
|
||||||
static_assert(sizeof(this->round_keys) == sizeof(::Aes256Context));
|
static_assert(sizeof(m_round_keys) == sizeof(::Aes256Context));
|
||||||
aes256ContextCreate(reinterpret_cast<Aes256Context *>(this->round_keys), key, is_encrypt);
|
aes256ContextCreate(reinterpret_cast<Aes256Context *>(m_round_keys), key, is_encrypt);
|
||||||
} else {
|
} else {
|
||||||
/* Invalid key size. */
|
/* Invalid key size. */
|
||||||
static_assert(!std::is_same<AesImpl<KeySize>, AesImpl<KeySize>>::value);
|
static_assert(!std::is_same<AesImpl<KeySize>, AesImpl<KeySize>>::value);
|
||||||
|
@ -65,16 +65,16 @@ namespace ams::crypto::impl {
|
||||||
|
|
||||||
if constexpr (KeySize == 16) {
|
if constexpr (KeySize == 16) {
|
||||||
/* Aes 128. */
|
/* Aes 128. */
|
||||||
static_assert(sizeof(this->round_keys) == sizeof(::Aes128Context));
|
static_assert(sizeof(m_round_keys) == sizeof(::Aes128Context));
|
||||||
aes128EncryptBlock(reinterpret_cast<const Aes128Context *>(this->round_keys), dst, src);
|
aes128EncryptBlock(reinterpret_cast<const Aes128Context *>(m_round_keys), dst, src);
|
||||||
} else if constexpr (KeySize == 24) {
|
} else if constexpr (KeySize == 24) {
|
||||||
/* Aes 192. */
|
/* Aes 192. */
|
||||||
static_assert(sizeof(this->round_keys) == sizeof(::Aes192Context));
|
static_assert(sizeof(m_round_keys) == sizeof(::Aes192Context));
|
||||||
aes192EncryptBlock(reinterpret_cast<const Aes192Context *>(this->round_keys), dst, src);
|
aes192EncryptBlock(reinterpret_cast<const Aes192Context *>(m_round_keys), dst, src);
|
||||||
} else if constexpr (KeySize == 32) {
|
} else if constexpr (KeySize == 32) {
|
||||||
/* Aes 256. */
|
/* Aes 256. */
|
||||||
static_assert(sizeof(this->round_keys) == sizeof(::Aes256Context));
|
static_assert(sizeof(m_round_keys) == sizeof(::Aes256Context));
|
||||||
aes256EncryptBlock(reinterpret_cast<const Aes256Context *>(this->round_keys), dst, src);
|
aes256EncryptBlock(reinterpret_cast<const Aes256Context *>(m_round_keys), dst, src);
|
||||||
} else {
|
} else {
|
||||||
/* Invalid key size. */
|
/* Invalid key size. */
|
||||||
static_assert(!std::is_same<AesImpl<KeySize>, AesImpl<KeySize>>::value);
|
static_assert(!std::is_same<AesImpl<KeySize>, AesImpl<KeySize>>::value);
|
||||||
|
@ -90,16 +90,16 @@ namespace ams::crypto::impl {
|
||||||
|
|
||||||
if constexpr (KeySize == 16) {
|
if constexpr (KeySize == 16) {
|
||||||
/* Aes 128. */
|
/* Aes 128. */
|
||||||
static_assert(sizeof(this->round_keys) == sizeof(::Aes128Context));
|
static_assert(sizeof(m_round_keys) == sizeof(::Aes128Context));
|
||||||
aes128DecryptBlock(reinterpret_cast<const Aes128Context *>(this->round_keys), dst, src);
|
aes128DecryptBlock(reinterpret_cast<const Aes128Context *>(m_round_keys), dst, src);
|
||||||
} else if constexpr (KeySize == 24) {
|
} else if constexpr (KeySize == 24) {
|
||||||
/* Aes 192. */
|
/* Aes 192. */
|
||||||
static_assert(sizeof(this->round_keys) == sizeof(::Aes192Context));
|
static_assert(sizeof(m_round_keys) == sizeof(::Aes192Context));
|
||||||
aes192DecryptBlock(reinterpret_cast<const Aes192Context *>(this->round_keys), dst, src);
|
aes192DecryptBlock(reinterpret_cast<const Aes192Context *>(m_round_keys), dst, src);
|
||||||
} else if constexpr (KeySize == 32) {
|
} else if constexpr (KeySize == 32) {
|
||||||
/* Aes 256. */
|
/* Aes 256. */
|
||||||
static_assert(sizeof(this->round_keys) == sizeof(::Aes256Context));
|
static_assert(sizeof(m_round_keys) == sizeof(::Aes256Context));
|
||||||
aes256DecryptBlock(reinterpret_cast<const Aes256Context *>(this->round_keys), dst, src);
|
aes256DecryptBlock(reinterpret_cast<const Aes256Context *>(m_round_keys), dst, src);
|
||||||
} else {
|
} else {
|
||||||
/* Invalid key size. */
|
/* Invalid key size. */
|
||||||
static_assert(!std::is_same<AesImpl<KeySize>, AesImpl<KeySize>>::value);
|
static_assert(!std::is_same<AesImpl<KeySize>, AesImpl<KeySize>>::value);
|
||||||
|
|
|
@ -54,13 +54,13 @@ namespace ams::crypto::impl {
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t BigNum::GetSize() const {
|
size_t BigNum::GetSize() const {
|
||||||
if (this->num_words == 0) {
|
if (m_num_words == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
static_assert(sizeof(Word) == 4);
|
static_assert(sizeof(Word) == 4);
|
||||||
|
|
||||||
size_t size = this->num_words * sizeof(Word);
|
size_t size = m_num_words * sizeof(Word);
|
||||||
const Word last = this->words[this->num_words - 1];
|
const Word last = m_words[m_num_words - 1];
|
||||||
AMS_ASSERT(last != 0);
|
AMS_ASSERT(last != 0);
|
||||||
if (last >= 0x01000000u) {
|
if (last >= 0x01000000u) {
|
||||||
return size - 0;
|
return size - 0;
|
||||||
|
@ -84,21 +84,21 @@ namespace ams::crypto::impl {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Ensure we have space for the number. */
|
/* Ensure we have space for the number. */
|
||||||
AMS_ASSERT(src_size <= this->max_words * sizeof(Word));
|
AMS_ASSERT(src_size <= m_max_words * sizeof(Word));
|
||||||
if (AMS_UNLIKELY(!(src_size <= this->max_words * sizeof(Word)))) {
|
if (AMS_UNLIKELY(!(src_size <= m_max_words * sizeof(Word)))) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Import. */
|
/* Import. */
|
||||||
this->num_words = util::AlignUp(src_size, sizeof(Word)) / sizeof(Word);
|
m_num_words = util::AlignUp(src_size, sizeof(Word)) / sizeof(Word);
|
||||||
|
|
||||||
ImportImpl(this->words, this->max_words, data, src_size);
|
ImportImpl(m_words, m_max_words, data, src_size);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BigNum::Export(void *dst, size_t dst_size) {
|
void BigNum::Export(void *dst, size_t dst_size) {
|
||||||
AMS_ASSERT(dst_size >= this->GetSize());
|
AMS_ASSERT(dst_size >= this->GetSize());
|
||||||
ExportImpl(static_cast<u8 *>(dst), dst_size, this->words, this->num_words);
|
ExportImpl(static_cast<u8 *>(dst), dst_size, m_words, m_num_words);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BigNum::ExpMod(void *dst, const void *src, size_t size, const BigNum &exp, u32 *work_buf, size_t work_buf_size) const {
|
bool BigNum::ExpMod(void *dst, const void *src, size_t size, const BigNum &exp, u32 *work_buf, size_t work_buf_size) const {
|
||||||
|
@ -126,7 +126,7 @@ namespace ams::crypto::impl {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Perform the exponentiation. */
|
/* Perform the exponentiation. */
|
||||||
if (!ExpMod(signature.words, signature.words, exp.words, exp.num_words, this->words, this->num_words, std::addressof(allocator))) {
|
if (!ExpMod(signature.m_words, signature.m_words, exp.m_words, exp.m_num_words, m_words, m_num_words, std::addressof(allocator))) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,11 +138,11 @@ namespace ams::crypto::impl {
|
||||||
}
|
}
|
||||||
|
|
||||||
void BigNum::ClearToZero() {
|
void BigNum::ClearToZero() {
|
||||||
std::memset(this->words, 0, this->num_words * sizeof(Word));
|
std::memset(m_words, 0, m_num_words * sizeof(Word));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BigNum::UpdateCount() {
|
void BigNum::UpdateCount() {
|
||||||
this->num_words = CountWords(this->words, this->max_words);
|
m_num_words = CountWords(m_words, m_max_words);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -89,7 +89,7 @@ namespace ams::crypto::impl {
|
||||||
template<>
|
template<>
|
||||||
void CtrModeImpl<AesEncryptor128>::ProcessBlocks(u8 *dst, const u8 *src, size_t num_blocks) {
|
void CtrModeImpl<AesEncryptor128>::ProcessBlocks(u8 *dst, const u8 *src, size_t num_blocks) {
|
||||||
/* Preload all round keys + iv into neon registers. */
|
/* Preload all round keys + iv into neon registers. */
|
||||||
const u8 *keys = this->block_cipher->GetRoundKey();
|
const u8 *keys = m_block_cipher->GetRoundKey();
|
||||||
DECLARE_ROUND_KEY_VAR(0);
|
DECLARE_ROUND_KEY_VAR(0);
|
||||||
DECLARE_ROUND_KEY_VAR(1);
|
DECLARE_ROUND_KEY_VAR(1);
|
||||||
DECLARE_ROUND_KEY_VAR(2);
|
DECLARE_ROUND_KEY_VAR(2);
|
||||||
|
@ -101,7 +101,7 @@ namespace ams::crypto::impl {
|
||||||
DECLARE_ROUND_KEY_VAR(8);
|
DECLARE_ROUND_KEY_VAR(8);
|
||||||
DECLARE_ROUND_KEY_VAR(9);
|
DECLARE_ROUND_KEY_VAR(9);
|
||||||
DECLARE_ROUND_KEY_VAR(10);
|
DECLARE_ROUND_KEY_VAR(10);
|
||||||
uint8x16_t ctr0 = vld1q_u8(this->counter);
|
uint8x16_t ctr0 = vld1q_u8(m_counter);
|
||||||
uint64_t high, low;
|
uint64_t high, low;
|
||||||
|
|
||||||
/* Process three blocks at a time, when possible. */
|
/* Process three blocks at a time, when possible. */
|
||||||
|
@ -237,13 +237,13 @@ namespace ams::crypto::impl {
|
||||||
num_blocks--;
|
num_blocks--;
|
||||||
}
|
}
|
||||||
|
|
||||||
vst1q_u8(this->counter, ctr0);
|
vst1q_u8(m_counter, ctr0);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
void CtrModeImpl<AesEncryptor192>::ProcessBlocks(u8 *dst, const u8 *src, size_t num_blocks) {
|
void CtrModeImpl<AesEncryptor192>::ProcessBlocks(u8 *dst, const u8 *src, size_t num_blocks) {
|
||||||
/* Preload all round keys + iv into neon registers. */
|
/* Preload all round keys + iv into neon registers. */
|
||||||
const u8 *keys = this->block_cipher->GetRoundKey();
|
const u8 *keys = m_block_cipher->GetRoundKey();
|
||||||
DECLARE_ROUND_KEY_VAR(0);
|
DECLARE_ROUND_KEY_VAR(0);
|
||||||
DECLARE_ROUND_KEY_VAR(1);
|
DECLARE_ROUND_KEY_VAR(1);
|
||||||
DECLARE_ROUND_KEY_VAR(2);
|
DECLARE_ROUND_KEY_VAR(2);
|
||||||
|
@ -257,7 +257,7 @@ namespace ams::crypto::impl {
|
||||||
DECLARE_ROUND_KEY_VAR(10);
|
DECLARE_ROUND_KEY_VAR(10);
|
||||||
DECLARE_ROUND_KEY_VAR(11);
|
DECLARE_ROUND_KEY_VAR(11);
|
||||||
DECLARE_ROUND_KEY_VAR(12);
|
DECLARE_ROUND_KEY_VAR(12);
|
||||||
uint8x16_t ctr0 = vld1q_u8(this->counter);
|
uint8x16_t ctr0 = vld1q_u8(m_counter);
|
||||||
uint64_t high, low;
|
uint64_t high, low;
|
||||||
|
|
||||||
/* Process three blocks at a time, when possible. */
|
/* Process three blocks at a time, when possible. */
|
||||||
|
@ -401,13 +401,13 @@ namespace ams::crypto::impl {
|
||||||
num_blocks--;
|
num_blocks--;
|
||||||
}
|
}
|
||||||
|
|
||||||
vst1q_u8(this->counter, ctr0);
|
vst1q_u8(m_counter, ctr0);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
void CtrModeImpl<AesEncryptor256>::ProcessBlocks(u8 *dst, const u8 *src, size_t num_blocks) {
|
void CtrModeImpl<AesEncryptor256>::ProcessBlocks(u8 *dst, const u8 *src, size_t num_blocks) {
|
||||||
/* Preload all round keys + iv into neon registers. */
|
/* Preload all round keys + iv into neon registers. */
|
||||||
const u8 *keys = this->block_cipher->GetRoundKey();
|
const u8 *keys = m_block_cipher->GetRoundKey();
|
||||||
DECLARE_ROUND_KEY_VAR(0);
|
DECLARE_ROUND_KEY_VAR(0);
|
||||||
DECLARE_ROUND_KEY_VAR(1);
|
DECLARE_ROUND_KEY_VAR(1);
|
||||||
DECLARE_ROUND_KEY_VAR(2);
|
DECLARE_ROUND_KEY_VAR(2);
|
||||||
|
@ -423,7 +423,7 @@ namespace ams::crypto::impl {
|
||||||
DECLARE_ROUND_KEY_VAR(12);
|
DECLARE_ROUND_KEY_VAR(12);
|
||||||
DECLARE_ROUND_KEY_VAR(13);
|
DECLARE_ROUND_KEY_VAR(13);
|
||||||
DECLARE_ROUND_KEY_VAR(14);
|
DECLARE_ROUND_KEY_VAR(14);
|
||||||
uint8x16_t ctr0 = vld1q_u8(this->counter);
|
uint8x16_t ctr0 = vld1q_u8(m_counter);
|
||||||
uint64_t high, low;
|
uint64_t high, low;
|
||||||
|
|
||||||
/* Process three blocks at a time, when possible. */
|
/* Process three blocks at a time, when possible. */
|
||||||
|
@ -576,7 +576,7 @@ namespace ams::crypto::impl {
|
||||||
num_blocks--;
|
num_blocks--;
|
||||||
}
|
}
|
||||||
|
|
||||||
vst1q_u8(this->counter, ctr0);
|
vst1q_u8(m_counter, ctr0);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,96 +108,96 @@ namespace ams::crypto::impl {
|
||||||
template<class BlockCipher>
|
template<class BlockCipher>
|
||||||
void GcmModeImpl<BlockCipher>::Initialize(const BlockCipher *block_cipher) {
|
void GcmModeImpl<BlockCipher>::Initialize(const BlockCipher *block_cipher) {
|
||||||
/* Set member variables. */
|
/* Set member variables. */
|
||||||
this->block_cipher = block_cipher;
|
m_block_cipher = block_cipher;
|
||||||
this->cipher_func = std::addressof(GcmModeImpl<BlockCipher>::ProcessBlock);
|
m_cipher_func = std::addressof(GcmModeImpl<BlockCipher>::ProcessBlock);
|
||||||
|
|
||||||
/* Pre-calculate values to speed up galois field multiplications later. */
|
/* Pre-calculate values to speed up galois field multiplications later. */
|
||||||
this->InitializeHashKey();
|
this->InitializeHashKey();
|
||||||
|
|
||||||
/* Note that we're initialized. */
|
/* Note that we're initialized. */
|
||||||
this->state = State_Initialized;
|
m_state = State_Initialized;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class BlockCipher>
|
template<class BlockCipher>
|
||||||
void GcmModeImpl<BlockCipher>::Reset(const void *iv, size_t iv_size) {
|
void GcmModeImpl<BlockCipher>::Reset(const void *iv, size_t iv_size) {
|
||||||
/* Validate pre-conditions. */
|
/* Validate pre-conditions. */
|
||||||
AMS_ASSERT(this->state >= State_Initialized);
|
AMS_ASSERT(m_state >= State_Initialized);
|
||||||
|
|
||||||
/* Reset blocks. */
|
/* Reset blocks. */
|
||||||
this->block_x.block_128.Clear();
|
m_block_x.block_128.Clear();
|
||||||
this->block_tmp.block_128.Clear();
|
m_block_tmp.block_128.Clear();
|
||||||
|
|
||||||
/* Clear sizes. */
|
/* Clear sizes. */
|
||||||
this->aad_size = 0;
|
m_aad_size = 0;
|
||||||
this->msg_size = 0;
|
m_msg_size = 0;
|
||||||
this->aad_remaining = 0;
|
m_aad_remaining = 0;
|
||||||
this->msg_remaining = 0;
|
m_msg_remaining = 0;
|
||||||
|
|
||||||
/* Update our state. */
|
/* Update our state. */
|
||||||
this->state = State_ProcessingAad;
|
m_state = State_ProcessingAad;
|
||||||
|
|
||||||
/* Set our iv. */
|
/* Set our iv. */
|
||||||
if (iv_size == 12) {
|
if (iv_size == 12) {
|
||||||
/* If our iv is the correct size, simply copy in the iv, and set the magic bit. */
|
/* If our iv is the correct size, simply copy in the iv, and set the magic bit. */
|
||||||
std::memcpy(std::addressof(this->block_ek0), iv, iv_size);
|
std::memcpy(std::addressof(m_block_ek0), iv, iv_size);
|
||||||
util::StoreBigEndian(this->block_ek0.block_32 + 3, static_cast<u32>(1));
|
util::StoreBigEndian(m_block_ek0.block_32 + 3, static_cast<u32>(1));
|
||||||
} else {
|
} else {
|
||||||
/* Clear our ek0 block. */
|
/* Clear our ek0 block. */
|
||||||
this->block_ek0.block_128.Clear();
|
m_block_ek0.block_128.Clear();
|
||||||
|
|
||||||
/* Update using the iv as aad. */
|
/* Update using the iv as aad. */
|
||||||
this->UpdateAad(iv, iv_size);
|
this->UpdateAad(iv, iv_size);
|
||||||
|
|
||||||
/* Treat the iv as fake msg for the mac that will become our iv. */
|
/* Treat the iv as fake msg for the mac that will become our iv. */
|
||||||
this->msg_size = this->aad_size;
|
m_msg_size = m_aad_size;
|
||||||
this->aad_size = 0;
|
m_aad_size = 0;
|
||||||
|
|
||||||
/* Compute a non-final mac. */
|
/* Compute a non-final mac. */
|
||||||
this->ComputeMac(false);
|
this->ComputeMac(false);
|
||||||
|
|
||||||
/* Set our ek0 block to our calculated mac block. */
|
/* Set our ek0 block to our calculated mac block. */
|
||||||
this->block_ek0 = this->block_x;
|
m_block_ek0 = m_block_x;
|
||||||
|
|
||||||
/* Clear our calculated mac block. */
|
/* Clear our calculated mac block. */
|
||||||
this->block_x.block_128.Clear();
|
m_block_x.block_128.Clear();
|
||||||
|
|
||||||
/* Reset our state. */
|
/* Reset our state. */
|
||||||
this->msg_size = 0;
|
m_msg_size = 0;
|
||||||
this->aad_size = 0;
|
m_aad_size = 0;
|
||||||
this->msg_remaining = 0;
|
m_msg_remaining = 0;
|
||||||
this->aad_remaining = 0;
|
m_aad_remaining = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set the working block to the iv. */
|
/* Set the working block to the iv. */
|
||||||
this->block_ek = this->block_ek0;
|
m_block_ek = m_block_ek0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class BlockCipher>
|
template<class BlockCipher>
|
||||||
void GcmModeImpl<BlockCipher>::UpdateAad(const void *aad, size_t aad_size) {
|
void GcmModeImpl<BlockCipher>::UpdateAad(const void *aad, size_t aad_size) {
|
||||||
/* Validate pre-conditions. */
|
/* Validate pre-conditions. */
|
||||||
AMS_ASSERT(this->state == State_ProcessingAad);
|
AMS_ASSERT(m_state == State_ProcessingAad);
|
||||||
AMS_ASSERT(this->msg_size == 0);
|
AMS_ASSERT(m_msg_size == 0);
|
||||||
|
|
||||||
/* Update our aad size. */
|
/* Update our aad size. */
|
||||||
this->aad_size += aad_size;
|
m_aad_size += aad_size;
|
||||||
|
|
||||||
/* Define a working tracker variable. */
|
/* Define a working tracker variable. */
|
||||||
const u8 *cur_aad = static_cast<const u8 *>(aad);
|
const u8 *cur_aad = static_cast<const u8 *>(aad);
|
||||||
|
|
||||||
/* Process any leftover aad data from a previous invocation. */
|
/* Process any leftover aad data from a previous invocation. */
|
||||||
if (this->aad_remaining > 0) {
|
if (m_aad_remaining > 0) {
|
||||||
while (aad_size > 0) {
|
while (aad_size > 0) {
|
||||||
/* Copy in a byte of the aad to our partial block. */
|
/* Copy in a byte of the aad to our partial block. */
|
||||||
this->block_x.block_8[this->aad_remaining] ^= *(cur_aad++);
|
m_block_x.block_8[m_aad_remaining] ^= *(cur_aad++);
|
||||||
|
|
||||||
/* Note that we consumed a byte. */
|
/* Note that we consumed a byte. */
|
||||||
--aad_size;
|
--aad_size;
|
||||||
|
|
||||||
/* Increment our partial block size. */
|
/* Increment our partial block size. */
|
||||||
this->aad_remaining = (this->aad_remaining + 1) % BlockSize;
|
m_aad_remaining = (m_aad_remaining + 1) % BlockSize;
|
||||||
|
|
||||||
/* If we have a complete block, process it and move onward. */
|
/* If we have a complete block, process it and move onward. */
|
||||||
GaloisFieldMult(std::addressof(this->block_x), std::addressof(this->block_x), std::addressof(this->h_mult_blocks[0]));
|
GaloisFieldMult(std::addressof(m_block_x), std::addressof(m_block_x), std::addressof(m_h_mult_blocks[0]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,11 +205,11 @@ namespace ams::crypto::impl {
|
||||||
while (aad_size >= BlockSize) {
|
while (aad_size >= BlockSize) {
|
||||||
/* Xor the current aad into our work block. */
|
/* Xor the current aad into our work block. */
|
||||||
for (size_t i = 0; i < BlockSize; ++i) {
|
for (size_t i = 0; i < BlockSize; ++i) {
|
||||||
this->block_x.block_8[i] ^= *(cur_aad++);
|
m_block_x.block_8[i] ^= *(cur_aad++);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Multiply the blocks in our galois field. */
|
/* Multiply the blocks in our galois field. */
|
||||||
GaloisFieldMult(std::addressof(this->block_x), std::addressof(this->block_x), std::addressof(this->h_mult_blocks[0]));
|
GaloisFieldMult(std::addressof(m_block_x), std::addressof(m_block_x), std::addressof(m_h_mult_blocks[0]));
|
||||||
|
|
||||||
/* Note that we've processed a block. */
|
/* Note that we've processed a block. */
|
||||||
aad_size -= BlockSize;
|
aad_size -= BlockSize;
|
||||||
|
@ -218,11 +218,11 @@ namespace ams::crypto::impl {
|
||||||
/* Update our state with whatever aad is left over. */
|
/* Update our state with whatever aad is left over. */
|
||||||
if (aad_size > 0) {
|
if (aad_size > 0) {
|
||||||
/* Note how much left over data we have. */
|
/* Note how much left over data we have. */
|
||||||
this->aad_remaining = static_cast<u32>(aad_size);
|
m_aad_remaining = static_cast<u32>(aad_size);
|
||||||
|
|
||||||
/* Xor the data in. */
|
/* Xor the data in. */
|
||||||
for (size_t i = 0; i < aad_size; ++i) {
|
for (size_t i = 0; i < aad_size; ++i) {
|
||||||
this->block_x.block_8[i] ^= *(cur_aad++);
|
m_block_x.block_8[i] ^= *(cur_aad++);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -234,21 +234,21 @@ namespace ams::crypto::impl {
|
||||||
template<class BlockCipher>
|
template<class BlockCipher>
|
||||||
void GcmModeImpl<BlockCipher>::GetMac(void *dst, size_t dst_size) {
|
void GcmModeImpl<BlockCipher>::GetMac(void *dst, size_t dst_size) {
|
||||||
/* Validate pre-conditions. */
|
/* Validate pre-conditions. */
|
||||||
AMS_ASSERT(State_ProcessingAad <= this->state && this->state <= State_Done);
|
AMS_ASSERT(State_ProcessingAad <= m_state && m_state <= State_Done);
|
||||||
AMS_ASSERT(dst != nullptr);
|
AMS_ASSERT(dst != nullptr);
|
||||||
AMS_ASSERT(dst_size >= MacSize);
|
AMS_ASSERT(dst_size >= MacSize);
|
||||||
AMS_ASSERT(this->aad_remaining == 0);
|
AMS_ASSERT(m_aad_remaining == 0);
|
||||||
AMS_ASSERT(this->msg_remaining == 0);
|
AMS_ASSERT(m_msg_remaining == 0);
|
||||||
AMS_UNUSED(dst_size);
|
AMS_UNUSED(dst_size);
|
||||||
|
|
||||||
/* If we haven't already done so, compute the final mac. */
|
/* If we haven't already done so, compute the final mac. */
|
||||||
if (this->state != State_Done) {
|
if (m_state != State_Done) {
|
||||||
this->ComputeMac(true);
|
this->ComputeMac(true);
|
||||||
this->state = State_Done;
|
m_state = State_Done;
|
||||||
}
|
}
|
||||||
|
|
||||||
static_assert(sizeof(this->block_x) == MacSize);
|
static_assert(sizeof(m_block_x) == MacSize);
|
||||||
std::memcpy(dst, std::addressof(this->block_x), MacSize);
|
std::memcpy(dst, std::addressof(m_block_x), MacSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class BlockCipher>
|
template<class BlockCipher>
|
||||||
|
@ -258,18 +258,18 @@ namespace ams::crypto::impl {
|
||||||
/* to speed up galois field arithmetic. */
|
/* to speed up galois field arithmetic. */
|
||||||
constexpr const Block EmptyBlock = {};
|
constexpr const Block EmptyBlock = {};
|
||||||
|
|
||||||
this->ProcessBlock(std::addressof(this->h_mult_blocks[0]), std::addressof(EmptyBlock), this->block_cipher);
|
this->ProcessBlock(std::addressof(m_h_mult_blocks[0]), std::addressof(EmptyBlock), m_block_cipher);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class BlockCipher>
|
template<class BlockCipher>
|
||||||
void GcmModeImpl<BlockCipher>::ComputeMac(bool encrypt) {
|
void GcmModeImpl<BlockCipher>::ComputeMac(bool encrypt) {
|
||||||
/* If we have leftover data, process it. */
|
/* If we have leftover data, process it. */
|
||||||
if (this->aad_remaining > 0 || this->msg_remaining > 0) {
|
if (m_aad_remaining > 0 || m_msg_remaining > 0) {
|
||||||
GaloisFieldMult(std::addressof(this->block_x), std::addressof(this->block_x), std::addressof(this->h_mult_blocks[0]));
|
GaloisFieldMult(std::addressof(m_block_x), std::addressof(m_block_x), std::addressof(m_h_mult_blocks[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Setup the last block. */
|
/* Setup the last block. */
|
||||||
Block last_block = Block{ .block_128 = { this->msg_size, this->aad_size } };
|
Block last_block = Block{ .block_128 = { m_msg_size, m_aad_size } };
|
||||||
|
|
||||||
/* Multiply the last block by 8 to account for bit vs byte sizes. */
|
/* Multiply the last block by 8 to account for bit vs byte sizes. */
|
||||||
static_assert(offsetof(Block128, hi) == 0);
|
static_assert(offsetof(Block128, hi) == 0);
|
||||||
|
@ -279,21 +279,21 @@ namespace ams::crypto::impl {
|
||||||
|
|
||||||
/* Xor the data in. */
|
/* Xor the data in. */
|
||||||
for (size_t i = 0; i < BlockSize; ++i) {
|
for (size_t i = 0; i < BlockSize; ++i) {
|
||||||
this->block_x.block_8[BlockSize - 1 - i] ^= last_block.block_8[i];
|
m_block_x.block_8[BlockSize - 1 - i] ^= last_block.block_8[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Perform the final multiplication. */
|
/* Perform the final multiplication. */
|
||||||
GaloisFieldMult(std::addressof(this->block_x), std::addressof(this->block_x), std::addressof(this->h_mult_blocks[0]));
|
GaloisFieldMult(std::addressof(m_block_x), std::addressof(m_block_x), std::addressof(m_h_mult_blocks[0]));
|
||||||
|
|
||||||
/* If we need to do an encryption, do so. */
|
/* If we need to do an encryption, do so. */
|
||||||
if (encrypt) {
|
if (encrypt) {
|
||||||
/* Encrypt the iv. */
|
/* Encrypt the iv. */
|
||||||
u8 enc_result[BlockSize];
|
u8 enc_result[BlockSize];
|
||||||
this->ProcessBlock(enc_result, std::addressof(this->block_ek0), this->block_cipher);
|
this->ProcessBlock(enc_result, std::addressof(m_block_ek0), m_block_cipher);
|
||||||
|
|
||||||
/* Xor the iv in. */
|
/* Xor the iv in. */
|
||||||
for (size_t i = 0; i < BlockSize; ++i) {
|
for (size_t i = 0; i < BlockSize; ++i) {
|
||||||
this->block_x.block_8[i] ^= enc_result[i];
|
m_block_x.block_8[i] ^= enc_result[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,20 +20,20 @@ namespace ams::crypto::impl {
|
||||||
#ifdef ATMOSPHERE_IS_STRATOSPHERE
|
#ifdef ATMOSPHERE_IS_STRATOSPHERE
|
||||||
|
|
||||||
void Sha1Impl::Initialize() {
|
void Sha1Impl::Initialize() {
|
||||||
static_assert(sizeof(this->state) == sizeof(::Sha1Context));
|
static_assert(sizeof(m_state) == sizeof(::Sha1Context));
|
||||||
::sha1ContextCreate(reinterpret_cast<::Sha1Context *>(std::addressof(this->state)));
|
::sha1ContextCreate(reinterpret_cast<::Sha1Context *>(std::addressof(m_state)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sha1Impl::Update(const void *data, size_t size) {
|
void Sha1Impl::Update(const void *data, size_t size) {
|
||||||
static_assert(sizeof(this->state) == sizeof(::Sha1Context));
|
static_assert(sizeof(m_state) == sizeof(::Sha1Context));
|
||||||
::sha1ContextUpdate(reinterpret_cast<::Sha1Context *>(std::addressof(this->state)), data, size);
|
::sha1ContextUpdate(reinterpret_cast<::Sha1Context *>(std::addressof(m_state)), data, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sha1Impl::GetHash(void *dst, size_t size) {
|
void Sha1Impl::GetHash(void *dst, size_t size) {
|
||||||
static_assert(sizeof(this->state) == sizeof(::Sha1Context));
|
static_assert(sizeof(m_state) == sizeof(::Sha1Context));
|
||||||
AMS_ASSERT(size >= HashSize);
|
AMS_ASSERT(size >= HashSize);
|
||||||
AMS_UNUSED(size);
|
AMS_UNUSED(size);
|
||||||
::sha1ContextGetHash(reinterpret_cast<::Sha1Context *>(std::addressof(this->state)), dst);
|
::sha1ContextGetHash(reinterpret_cast<::Sha1Context *>(std::addressof(m_state)), dst);
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
|
@ -20,42 +20,42 @@ namespace ams::crypto::impl {
|
||||||
#ifdef ATMOSPHERE_IS_STRATOSPHERE
|
#ifdef ATMOSPHERE_IS_STRATOSPHERE
|
||||||
|
|
||||||
void Sha256Impl::Initialize() {
|
void Sha256Impl::Initialize() {
|
||||||
static_assert(sizeof(this->state) == sizeof(::Sha256Context));
|
static_assert(sizeof(m_state) == sizeof(::Sha256Context));
|
||||||
::sha256ContextCreate(reinterpret_cast<::Sha256Context *>(std::addressof(this->state)));
|
::sha256ContextCreate(reinterpret_cast<::Sha256Context *>(std::addressof(m_state)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sha256Impl::Update(const void *data, size_t size) {
|
void Sha256Impl::Update(const void *data, size_t size) {
|
||||||
static_assert(sizeof(this->state) == sizeof(::Sha256Context));
|
static_assert(sizeof(m_state) == sizeof(::Sha256Context));
|
||||||
::sha256ContextUpdate(reinterpret_cast<::Sha256Context *>(std::addressof(this->state)), data, size);
|
::sha256ContextUpdate(reinterpret_cast<::Sha256Context *>(std::addressof(m_state)), data, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sha256Impl::GetHash(void *dst, size_t size) {
|
void Sha256Impl::GetHash(void *dst, size_t size) {
|
||||||
static_assert(sizeof(this->state) == sizeof(::Sha256Context));
|
static_assert(sizeof(m_state) == sizeof(::Sha256Context));
|
||||||
AMS_ASSERT(size >= HashSize);
|
AMS_ASSERT(size >= HashSize);
|
||||||
AMS_UNUSED(size);
|
AMS_UNUSED(size);
|
||||||
|
|
||||||
::sha256ContextGetHash(reinterpret_cast<::Sha256Context *>(std::addressof(this->state)), dst);
|
::sha256ContextGetHash(reinterpret_cast<::Sha256Context *>(std::addressof(m_state)), dst);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sha256Impl::InitializeWithContext(const Sha256Context *context) {
|
void Sha256Impl::InitializeWithContext(const Sha256Context *context) {
|
||||||
static_assert(sizeof(this->state) == sizeof(::Sha256Context));
|
static_assert(sizeof(m_state) == sizeof(::Sha256Context));
|
||||||
|
|
||||||
/* Copy state in from the context. */
|
/* Copy state in from the context. */
|
||||||
std::memcpy(this->state.intermediate_hash, context->intermediate_hash, sizeof(this->state.intermediate_hash));
|
std::memcpy(m_state.intermediate_hash, context->intermediate_hash, sizeof(m_state.intermediate_hash));
|
||||||
this->state.bits_consumed = context->bits_consumed;
|
m_state.bits_consumed = context->bits_consumed;
|
||||||
|
|
||||||
/* Clear the rest of state. */
|
/* Clear the rest of state. */
|
||||||
std::memset(this->state.buffer, 0, sizeof(this->state.buffer));
|
std::memset(m_state.buffer, 0, sizeof(m_state.buffer));
|
||||||
this->state.num_buffered = 0;
|
m_state.num_buffered = 0;
|
||||||
this->state.finalized = false;
|
m_state.finalized = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Sha256Impl::GetContext(Sha256Context *context) const {
|
size_t Sha256Impl::GetContext(Sha256Context *context) const {
|
||||||
static_assert(sizeof(this->state) == sizeof(::Sha256Context));
|
static_assert(sizeof(m_state) == sizeof(::Sha256Context));
|
||||||
std::memcpy(context->intermediate_hash, this->state.intermediate_hash, sizeof(context->intermediate_hash));
|
std::memcpy(context->intermediate_hash, m_state.intermediate_hash, sizeof(context->intermediate_hash));
|
||||||
context->bits_consumed = this->state.bits_consumed;
|
context->bits_consumed = m_state.bits_consumed;
|
||||||
|
|
||||||
return this->state.num_buffered;
|
return m_state.num_buffered;
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
|
@ -110,7 +110,7 @@ namespace ams::crypto::impl {
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t XtsModeImpl::UpdateGeneric(void *dst, size_t dst_size, const void *src, size_t src_size) {
|
size_t XtsModeImpl::UpdateGeneric(void *dst, size_t dst_size, const void *src, size_t src_size) {
|
||||||
AMS_ASSERT(this->state == State_Initialized || this->state == State_Processing);
|
AMS_ASSERT(m_state == State_Initialized || m_state == State_Processing);
|
||||||
|
|
||||||
return UpdateImpl<void>(this, dst, dst_size, src, src_size);
|
return UpdateImpl<void>(this, dst, dst_size, src, src_size);
|
||||||
}
|
}
|
||||||
|
@ -118,13 +118,13 @@ namespace ams::crypto::impl {
|
||||||
size_t XtsModeImpl::ProcessBlocksGeneric(u8 *dst, const u8 *src, size_t num_blocks) {
|
size_t XtsModeImpl::ProcessBlocksGeneric(u8 *dst, const u8 *src, size_t num_blocks) {
|
||||||
size_t processed = BlockSize * (num_blocks - 1);
|
size_t processed = BlockSize * (num_blocks - 1);
|
||||||
|
|
||||||
if (this->state == State_Processing) {
|
if (m_state == State_Processing) {
|
||||||
this->ProcessBlock(dst, this->last_block);
|
this->ProcessBlock(dst, m_last_block);
|
||||||
dst += BlockSize;
|
dst += BlockSize;
|
||||||
processed += BlockSize;
|
processed += BlockSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8x16_t tweak = vld1q_u8(this->tweak);
|
uint8x16_t tweak = vld1q_u8(m_tweak);
|
||||||
|
|
||||||
while ((--num_blocks) > 0) {
|
while ((--num_blocks) > 0) {
|
||||||
/* Xor */
|
/* Xor */
|
||||||
|
@ -134,7 +134,7 @@ namespace ams::crypto::impl {
|
||||||
|
|
||||||
/* Encrypt */
|
/* Encrypt */
|
||||||
vst1q_u8(dst, block);
|
vst1q_u8(dst, block);
|
||||||
this->cipher_func(dst, dst, this->cipher_ctx);
|
m_cipher_func(dst, dst, m_cipher_ctx);
|
||||||
block = vld1q_u8(dst);
|
block = vld1q_u8(dst);
|
||||||
|
|
||||||
/* Xor */
|
/* Xor */
|
||||||
|
@ -146,11 +146,11 @@ namespace ams::crypto::impl {
|
||||||
tweak = MultiplyTweak(tweak);
|
tweak = MultiplyTweak(tweak);
|
||||||
}
|
}
|
||||||
|
|
||||||
vst1q_u8(this->tweak, tweak);
|
vst1q_u8(m_tweak, tweak);
|
||||||
|
|
||||||
std::memcpy(this->last_block, src, BlockSize);
|
std::memcpy(m_last_block, src, BlockSize);
|
||||||
|
|
||||||
this->state = State_Processing;
|
m_state = State_Processing;
|
||||||
|
|
||||||
return processed;
|
return processed;
|
||||||
}
|
}
|
||||||
|
@ -168,14 +168,14 @@ namespace ams::crypto::impl {
|
||||||
/* Handle last buffered block. */
|
/* Handle last buffered block. */
|
||||||
size_t processed = (num_blocks - 1) * BlockSize;
|
size_t processed = (num_blocks - 1) * BlockSize;
|
||||||
|
|
||||||
if (this->state == State_Processing) {
|
if (m_state == State_Processing) {
|
||||||
this->ProcessBlock(dst, this->last_block);
|
this->ProcessBlock(dst, m_last_block);
|
||||||
dst += BlockSize;
|
dst += BlockSize;
|
||||||
processed += BlockSize;
|
processed += BlockSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Preload all round keys + iv into neon registers. */
|
/* Preload all round keys + iv into neon registers. */
|
||||||
const u8 *keys = static_cast<const AesEncryptor128 *>(this->cipher_ctx)->GetRoundKey();
|
const u8 *keys = static_cast<const AesEncryptor128 *>(m_cipher_ctx)->GetRoundKey();
|
||||||
DECLARE_ROUND_KEY_VAR(0);
|
DECLARE_ROUND_KEY_VAR(0);
|
||||||
DECLARE_ROUND_KEY_VAR(1);
|
DECLARE_ROUND_KEY_VAR(1);
|
||||||
DECLARE_ROUND_KEY_VAR(2);
|
DECLARE_ROUND_KEY_VAR(2);
|
||||||
|
@ -187,7 +187,7 @@ namespace ams::crypto::impl {
|
||||||
DECLARE_ROUND_KEY_VAR(8);
|
DECLARE_ROUND_KEY_VAR(8);
|
||||||
DECLARE_ROUND_KEY_VAR(9);
|
DECLARE_ROUND_KEY_VAR(9);
|
||||||
DECLARE_ROUND_KEY_VAR(10);
|
DECLARE_ROUND_KEY_VAR(10);
|
||||||
uint8x16_t tweak0 = vld1q_u8(this->tweak);
|
uint8x16_t tweak0 = vld1q_u8(m_tweak);
|
||||||
constexpr uint64_t xorv = 0x87ul;
|
constexpr uint64_t xorv = 0x87ul;
|
||||||
uint64_t high, low, mask;
|
uint64_t high, low, mask;
|
||||||
|
|
||||||
|
@ -314,10 +314,10 @@ namespace ams::crypto::impl {
|
||||||
dst += BlockSize;
|
dst += BlockSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
vst1q_u8(this->tweak, tweak0);
|
vst1q_u8(m_tweak, tweak0);
|
||||||
|
|
||||||
std::memcpy(this->last_block, src, BlockSize);
|
std::memcpy(m_last_block, src, BlockSize);
|
||||||
this->state = State_Processing;
|
m_state = State_Processing;
|
||||||
|
|
||||||
return processed;
|
return processed;
|
||||||
}
|
}
|
||||||
|
@ -327,14 +327,14 @@ namespace ams::crypto::impl {
|
||||||
/* Handle last buffered block. */
|
/* Handle last buffered block. */
|
||||||
size_t processed = (num_blocks - 1) * BlockSize;
|
size_t processed = (num_blocks - 1) * BlockSize;
|
||||||
|
|
||||||
if (this->state == State_Processing) {
|
if (m_state == State_Processing) {
|
||||||
this->ProcessBlock(dst, this->last_block);
|
this->ProcessBlock(dst, m_last_block);
|
||||||
dst += BlockSize;
|
dst += BlockSize;
|
||||||
processed += BlockSize;
|
processed += BlockSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Preload all round keys + iv into neon registers. */
|
/* Preload all round keys + iv into neon registers. */
|
||||||
const u8 *keys = static_cast<const AesEncryptor192 *>(this->cipher_ctx)->GetRoundKey();
|
const u8 *keys = static_cast<const AesEncryptor192 *>(m_cipher_ctx)->GetRoundKey();
|
||||||
DECLARE_ROUND_KEY_VAR(0);
|
DECLARE_ROUND_KEY_VAR(0);
|
||||||
DECLARE_ROUND_KEY_VAR(1);
|
DECLARE_ROUND_KEY_VAR(1);
|
||||||
DECLARE_ROUND_KEY_VAR(2);
|
DECLARE_ROUND_KEY_VAR(2);
|
||||||
|
@ -348,7 +348,7 @@ namespace ams::crypto::impl {
|
||||||
DECLARE_ROUND_KEY_VAR(10);
|
DECLARE_ROUND_KEY_VAR(10);
|
||||||
DECLARE_ROUND_KEY_VAR(11);
|
DECLARE_ROUND_KEY_VAR(11);
|
||||||
DECLARE_ROUND_KEY_VAR(12);
|
DECLARE_ROUND_KEY_VAR(12);
|
||||||
uint8x16_t tweak0 = vld1q_u8(this->tweak);
|
uint8x16_t tweak0 = vld1q_u8(m_tweak);
|
||||||
constexpr uint64_t xorv = 0x87ul;
|
constexpr uint64_t xorv = 0x87ul;
|
||||||
uint64_t high, low, mask;
|
uint64_t high, low, mask;
|
||||||
|
|
||||||
|
@ -483,10 +483,10 @@ namespace ams::crypto::impl {
|
||||||
dst += BlockSize;
|
dst += BlockSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
vst1q_u8(this->tweak, tweak0);
|
vst1q_u8(m_tweak, tweak0);
|
||||||
|
|
||||||
std::memcpy(this->last_block, src, BlockSize);
|
std::memcpy(m_last_block, src, BlockSize);
|
||||||
this->state = State_Processing;
|
m_state = State_Processing;
|
||||||
|
|
||||||
return processed;
|
return processed;
|
||||||
}
|
}
|
||||||
|
@ -496,14 +496,14 @@ namespace ams::crypto::impl {
|
||||||
/* Handle last buffered block. */
|
/* Handle last buffered block. */
|
||||||
size_t processed = (num_blocks - 1) * BlockSize;
|
size_t processed = (num_blocks - 1) * BlockSize;
|
||||||
|
|
||||||
if (this->state == State_Processing) {
|
if (m_state == State_Processing) {
|
||||||
this->ProcessBlock(dst, this->last_block);
|
this->ProcessBlock(dst, m_last_block);
|
||||||
dst += BlockSize;
|
dst += BlockSize;
|
||||||
processed += BlockSize;
|
processed += BlockSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Preload all round keys + iv into neon registers. */
|
/* Preload all round keys + iv into neon registers. */
|
||||||
const u8 *keys = static_cast<const AesEncryptor256 *>(this->cipher_ctx)->GetRoundKey();
|
const u8 *keys = static_cast<const AesEncryptor256 *>(m_cipher_ctx)->GetRoundKey();
|
||||||
DECLARE_ROUND_KEY_VAR(0);
|
DECLARE_ROUND_KEY_VAR(0);
|
||||||
DECLARE_ROUND_KEY_VAR(1);
|
DECLARE_ROUND_KEY_VAR(1);
|
||||||
DECLARE_ROUND_KEY_VAR(2);
|
DECLARE_ROUND_KEY_VAR(2);
|
||||||
|
@ -519,7 +519,7 @@ namespace ams::crypto::impl {
|
||||||
DECLARE_ROUND_KEY_VAR(12);
|
DECLARE_ROUND_KEY_VAR(12);
|
||||||
DECLARE_ROUND_KEY_VAR(13);
|
DECLARE_ROUND_KEY_VAR(13);
|
||||||
DECLARE_ROUND_KEY_VAR(14);
|
DECLARE_ROUND_KEY_VAR(14);
|
||||||
uint8x16_t tweak0 = vld1q_u8(this->tweak);
|
uint8x16_t tweak0 = vld1q_u8(m_tweak);
|
||||||
constexpr uint64_t xorv = 0x87ul;
|
constexpr uint64_t xorv = 0x87ul;
|
||||||
uint64_t high, low, mask;
|
uint64_t high, low, mask;
|
||||||
|
|
||||||
|
@ -663,10 +663,10 @@ namespace ams::crypto::impl {
|
||||||
dst += BlockSize;
|
dst += BlockSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
vst1q_u8(this->tweak, tweak0);
|
vst1q_u8(m_tweak, tweak0);
|
||||||
|
|
||||||
std::memcpy(this->last_block, src, BlockSize);
|
std::memcpy(m_last_block, src, BlockSize);
|
||||||
this->state = State_Processing;
|
m_state = State_Processing;
|
||||||
|
|
||||||
return processed;
|
return processed;
|
||||||
}
|
}
|
||||||
|
@ -676,14 +676,14 @@ namespace ams::crypto::impl {
|
||||||
/* Handle last buffered block. */
|
/* Handle last buffered block. */
|
||||||
size_t processed = (num_blocks - 1) * BlockSize;
|
size_t processed = (num_blocks - 1) * BlockSize;
|
||||||
|
|
||||||
if (this->state == State_Processing) {
|
if (m_state == State_Processing) {
|
||||||
this->ProcessBlock(dst, this->last_block);
|
this->ProcessBlock(dst, m_last_block);
|
||||||
dst += BlockSize;
|
dst += BlockSize;
|
||||||
processed += BlockSize;
|
processed += BlockSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Preload all round keys + iv into neon registers. */
|
/* Preload all round keys + iv into neon registers. */
|
||||||
const u8 *keys = static_cast<const AesDecryptor128 *>(this->cipher_ctx)->GetRoundKey();
|
const u8 *keys = static_cast<const AesDecryptor128 *>(m_cipher_ctx)->GetRoundKey();
|
||||||
DECLARE_ROUND_KEY_VAR(0);
|
DECLARE_ROUND_KEY_VAR(0);
|
||||||
DECLARE_ROUND_KEY_VAR(1);
|
DECLARE_ROUND_KEY_VAR(1);
|
||||||
DECLARE_ROUND_KEY_VAR(2);
|
DECLARE_ROUND_KEY_VAR(2);
|
||||||
|
@ -695,7 +695,7 @@ namespace ams::crypto::impl {
|
||||||
DECLARE_ROUND_KEY_VAR(8);
|
DECLARE_ROUND_KEY_VAR(8);
|
||||||
DECLARE_ROUND_KEY_VAR(9);
|
DECLARE_ROUND_KEY_VAR(9);
|
||||||
DECLARE_ROUND_KEY_VAR(10);
|
DECLARE_ROUND_KEY_VAR(10);
|
||||||
uint8x16_t tweak0 = vld1q_u8(this->tweak);
|
uint8x16_t tweak0 = vld1q_u8(m_tweak);
|
||||||
constexpr uint64_t xorv = 0x87ul;
|
constexpr uint64_t xorv = 0x87ul;
|
||||||
uint64_t high, low, mask;
|
uint64_t high, low, mask;
|
||||||
|
|
||||||
|
@ -822,10 +822,10 @@ namespace ams::crypto::impl {
|
||||||
dst += BlockSize;
|
dst += BlockSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
vst1q_u8(this->tweak, tweak0);
|
vst1q_u8(m_tweak, tweak0);
|
||||||
|
|
||||||
std::memcpy(this->last_block, src, BlockSize);
|
std::memcpy(m_last_block, src, BlockSize);
|
||||||
this->state = State_Processing;
|
m_state = State_Processing;
|
||||||
|
|
||||||
return processed;
|
return processed;
|
||||||
}
|
}
|
||||||
|
@ -835,14 +835,14 @@ namespace ams::crypto::impl {
|
||||||
/* Handle last buffered block. */
|
/* Handle last buffered block. */
|
||||||
size_t processed = (num_blocks - 1) * BlockSize;
|
size_t processed = (num_blocks - 1) * BlockSize;
|
||||||
|
|
||||||
if (this->state == State_Processing) {
|
if (m_state == State_Processing) {
|
||||||
this->ProcessBlock(dst, this->last_block);
|
this->ProcessBlock(dst, m_last_block);
|
||||||
dst += BlockSize;
|
dst += BlockSize;
|
||||||
processed += BlockSize;
|
processed += BlockSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Preload all round keys + iv into neon registers. */
|
/* Preload all round keys + iv into neon registers. */
|
||||||
const u8 *keys = static_cast<const AesDecryptor192 *>(this->cipher_ctx)->GetRoundKey();
|
const u8 *keys = static_cast<const AesDecryptor192 *>(m_cipher_ctx)->GetRoundKey();
|
||||||
DECLARE_ROUND_KEY_VAR(0);
|
DECLARE_ROUND_KEY_VAR(0);
|
||||||
DECLARE_ROUND_KEY_VAR(1);
|
DECLARE_ROUND_KEY_VAR(1);
|
||||||
DECLARE_ROUND_KEY_VAR(2);
|
DECLARE_ROUND_KEY_VAR(2);
|
||||||
|
@ -856,7 +856,7 @@ namespace ams::crypto::impl {
|
||||||
DECLARE_ROUND_KEY_VAR(10);
|
DECLARE_ROUND_KEY_VAR(10);
|
||||||
DECLARE_ROUND_KEY_VAR(11);
|
DECLARE_ROUND_KEY_VAR(11);
|
||||||
DECLARE_ROUND_KEY_VAR(12);
|
DECLARE_ROUND_KEY_VAR(12);
|
||||||
uint8x16_t tweak0 = vld1q_u8(this->tweak);
|
uint8x16_t tweak0 = vld1q_u8(m_tweak);
|
||||||
constexpr uint64_t xorv = 0x87ul;
|
constexpr uint64_t xorv = 0x87ul;
|
||||||
uint64_t high, low, mask;
|
uint64_t high, low, mask;
|
||||||
|
|
||||||
|
@ -991,10 +991,10 @@ namespace ams::crypto::impl {
|
||||||
dst += BlockSize;
|
dst += BlockSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
vst1q_u8(this->tweak, tweak0);
|
vst1q_u8(m_tweak, tweak0);
|
||||||
|
|
||||||
std::memcpy(this->last_block, src, BlockSize);
|
std::memcpy(m_last_block, src, BlockSize);
|
||||||
this->state = State_Processing;
|
m_state = State_Processing;
|
||||||
|
|
||||||
return processed;
|
return processed;
|
||||||
}
|
}
|
||||||
|
@ -1004,14 +1004,14 @@ namespace ams::crypto::impl {
|
||||||
/* Handle last buffered block. */
|
/* Handle last buffered block. */
|
||||||
size_t processed = (num_blocks - 1) * BlockSize;
|
size_t processed = (num_blocks - 1) * BlockSize;
|
||||||
|
|
||||||
if (this->state == State_Processing) {
|
if (m_state == State_Processing) {
|
||||||
this->ProcessBlock(dst, this->last_block);
|
this->ProcessBlock(dst, m_last_block);
|
||||||
dst += BlockSize;
|
dst += BlockSize;
|
||||||
processed += BlockSize;
|
processed += BlockSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Preload all round keys + iv into neon registers. */
|
/* Preload all round keys + iv into neon registers. */
|
||||||
const u8 *keys = static_cast<const AesDecryptor256 *>(this->cipher_ctx)->GetRoundKey();
|
const u8 *keys = static_cast<const AesDecryptor256 *>(m_cipher_ctx)->GetRoundKey();
|
||||||
DECLARE_ROUND_KEY_VAR(0);
|
DECLARE_ROUND_KEY_VAR(0);
|
||||||
DECLARE_ROUND_KEY_VAR(1);
|
DECLARE_ROUND_KEY_VAR(1);
|
||||||
DECLARE_ROUND_KEY_VAR(2);
|
DECLARE_ROUND_KEY_VAR(2);
|
||||||
|
@ -1027,7 +1027,7 @@ namespace ams::crypto::impl {
|
||||||
DECLARE_ROUND_KEY_VAR(12);
|
DECLARE_ROUND_KEY_VAR(12);
|
||||||
DECLARE_ROUND_KEY_VAR(13);
|
DECLARE_ROUND_KEY_VAR(13);
|
||||||
DECLARE_ROUND_KEY_VAR(14);
|
DECLARE_ROUND_KEY_VAR(14);
|
||||||
uint8x16_t tweak0 = vld1q_u8(this->tweak);
|
uint8x16_t tweak0 = vld1q_u8(m_tweak);
|
||||||
constexpr uint64_t xorv = 0x87ul;
|
constexpr uint64_t xorv = 0x87ul;
|
||||||
uint64_t high, low, mask;
|
uint64_t high, low, mask;
|
||||||
|
|
||||||
|
@ -1171,10 +1171,10 @@ namespace ams::crypto::impl {
|
||||||
dst += BlockSize;
|
dst += BlockSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
vst1q_u8(this->tweak, tweak0);
|
vst1q_u8(m_tweak, tweak0);
|
||||||
|
|
||||||
std::memcpy(this->last_block, src, BlockSize);
|
std::memcpy(m_last_block, src, BlockSize);
|
||||||
this->state = State_Processing;
|
m_state = State_Processing;
|
||||||
|
|
||||||
return processed;
|
return processed;
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,94 +39,94 @@ namespace ams::crypto::impl {
|
||||||
|
|
||||||
/* Xor. */
|
/* Xor. */
|
||||||
for (size_t i = 0; i < BlockSize; i++) {
|
for (size_t i = 0; i < BlockSize; i++) {
|
||||||
tmp[i] = this->tweak[i] ^ src[i];
|
tmp[i] = m_tweak[i] ^ src[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Crypt */
|
/* Crypt */
|
||||||
this->cipher_func(tmp, tmp, this->cipher_ctx);
|
m_cipher_func(tmp, tmp, m_cipher_ctx);
|
||||||
|
|
||||||
/* Xor. */
|
/* Xor. */
|
||||||
for (size_t i = 0; i < BlockSize; i++) {
|
for (size_t i = 0; i < BlockSize; i++) {
|
||||||
dst[i] = this->tweak[i] ^ tmp[i];
|
dst[i] = m_tweak[i] ^ tmp[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
MultiplyTweakGeneric(reinterpret_cast<u64 *>(this->tweak));
|
MultiplyTweakGeneric(reinterpret_cast<u64 *>(m_tweak));
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t XtsModeImpl::FinalizeEncryption(void *dst, size_t dst_size) {
|
size_t XtsModeImpl::FinalizeEncryption(void *dst, size_t dst_size) {
|
||||||
AMS_ASSERT(this->state == State_Processing);
|
AMS_ASSERT(m_state == State_Processing);
|
||||||
AMS_UNUSED(dst_size);
|
AMS_UNUSED(dst_size);
|
||||||
|
|
||||||
u8 *dst_u8 = static_cast<u8 *>(dst);
|
u8 *dst_u8 = static_cast<u8 *>(dst);
|
||||||
size_t processed = 0;
|
size_t processed = 0;
|
||||||
|
|
||||||
if (this->num_buffered == 0) {
|
if (m_num_buffered == 0) {
|
||||||
this->ProcessBlock(dst_u8, this->last_block);
|
this->ProcessBlock(dst_u8, m_last_block);
|
||||||
processed = BlockSize;
|
processed = BlockSize;
|
||||||
} else {
|
} else {
|
||||||
this->ProcessBlock(this->last_block, this->last_block);
|
this->ProcessBlock(m_last_block, m_last_block);
|
||||||
|
|
||||||
std::memcpy(this->buffer + this->num_buffered, this->last_block + this->num_buffered, BlockSize - this->num_buffered);
|
std::memcpy(m_buffer + m_num_buffered, m_last_block + m_num_buffered, BlockSize - m_num_buffered);
|
||||||
|
|
||||||
this->ProcessBlock(dst_u8, this->buffer);
|
this->ProcessBlock(dst_u8, m_buffer);
|
||||||
|
|
||||||
std::memcpy(dst_u8 + BlockSize, this->last_block, this->num_buffered);
|
std::memcpy(dst_u8 + BlockSize, m_last_block, m_num_buffered);
|
||||||
|
|
||||||
processed = BlockSize + this->num_buffered;
|
processed = BlockSize + m_num_buffered;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->state = State_Done;
|
m_state = State_Done;
|
||||||
return processed;
|
return processed;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t XtsModeImpl::FinalizeDecryption(void *dst, size_t dst_size) {
|
size_t XtsModeImpl::FinalizeDecryption(void *dst, size_t dst_size) {
|
||||||
AMS_ASSERT(this->state == State_Processing);
|
AMS_ASSERT(m_state == State_Processing);
|
||||||
AMS_UNUSED(dst_size);
|
AMS_UNUSED(dst_size);
|
||||||
|
|
||||||
u8 *dst_u8 = static_cast<u8 *>(dst);
|
u8 *dst_u8 = static_cast<u8 *>(dst);
|
||||||
size_t processed = 0;
|
size_t processed = 0;
|
||||||
|
|
||||||
if (this->num_buffered == 0) {
|
if (m_num_buffered == 0) {
|
||||||
this->ProcessBlock(dst_u8, this->last_block);
|
this->ProcessBlock(dst_u8, m_last_block);
|
||||||
processed = BlockSize;
|
processed = BlockSize;
|
||||||
} else {
|
} else {
|
||||||
u8 tmp_tweak[BlockSize];
|
u8 tmp_tweak[BlockSize];
|
||||||
std::memcpy(tmp_tweak, this->tweak, BlockSize);
|
std::memcpy(tmp_tweak, m_tweak, BlockSize);
|
||||||
MultiplyTweakGeneric(reinterpret_cast<u64 *>(this->tweak));
|
MultiplyTweakGeneric(reinterpret_cast<u64 *>(m_tweak));
|
||||||
|
|
||||||
this->ProcessBlock(this->last_block, this->last_block);
|
this->ProcessBlock(m_last_block, m_last_block);
|
||||||
|
|
||||||
std::memcpy(this->buffer + this->num_buffered, this->last_block + this->num_buffered, BlockSize - this->num_buffered);
|
std::memcpy(m_buffer + m_num_buffered, m_last_block + m_num_buffered, BlockSize - m_num_buffered);
|
||||||
|
|
||||||
std::memcpy(this->tweak, tmp_tweak, BlockSize);
|
std::memcpy(m_tweak, tmp_tweak, BlockSize);
|
||||||
|
|
||||||
this->ProcessBlock(dst_u8, this->buffer);
|
this->ProcessBlock(dst_u8, m_buffer);
|
||||||
|
|
||||||
std::memcpy(dst_u8 + BlockSize, this->last_block, this->num_buffered);
|
std::memcpy(dst_u8 + BlockSize, m_last_block, m_num_buffered);
|
||||||
|
|
||||||
processed = BlockSize + this->num_buffered;
|
processed = BlockSize + m_num_buffered;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->state = State_Done;
|
m_state = State_Done;
|
||||||
return processed;
|
return processed;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t XtsModeImpl::ProcessPartialData(u8 *dst, const u8 *src, size_t size) {
|
size_t XtsModeImpl::ProcessPartialData(u8 *dst, const u8 *src, size_t size) {
|
||||||
size_t processed = 0;
|
size_t processed = 0;
|
||||||
|
|
||||||
std::memcpy(this->buffer + this->num_buffered, src, size);
|
std::memcpy(m_buffer + m_num_buffered, src, size);
|
||||||
this->num_buffered += size;
|
m_num_buffered += size;
|
||||||
|
|
||||||
if (this->num_buffered == BlockSize) {
|
if (m_num_buffered == BlockSize) {
|
||||||
if (this->state == State_Processing) {
|
if (m_state == State_Processing) {
|
||||||
this->ProcessBlock(dst, this->last_block);
|
this->ProcessBlock(dst, m_last_block);
|
||||||
processed += BlockSize;
|
processed += BlockSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::memcpy(this->last_block, this->buffer, BlockSize);
|
std::memcpy(m_last_block, m_buffer, BlockSize);
|
||||||
this->num_buffered = 0;
|
m_num_buffered = 0;
|
||||||
|
|
||||||
this->state = State_Processing;
|
m_state = State_Processing;
|
||||||
}
|
}
|
||||||
|
|
||||||
return processed;
|
return processed;
|
||||||
|
@ -135,8 +135,8 @@ namespace ams::crypto::impl {
|
||||||
size_t XtsModeImpl::ProcessRemainingData(u8 *dst, const u8 *src, size_t size) {
|
size_t XtsModeImpl::ProcessRemainingData(u8 *dst, const u8 *src, size_t size) {
|
||||||
AMS_UNUSED(dst);
|
AMS_UNUSED(dst);
|
||||||
|
|
||||||
std::memcpy(this->buffer, src, size);
|
std::memcpy(m_buffer, src, size);
|
||||||
this->num_buffered = size;
|
m_num_buffered = size;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue