ams: support building unit test programs on windows/linux/macos

This commit is contained in:
Michael Scire
2022-03-06 12:08:20 -08:00
committed by SciresM
parent 9a38be201a
commit 64a97576d0
756 changed files with 33359 additions and 9372 deletions

View File

@@ -22,6 +22,10 @@ namespace ams::crypto {
using HmacSha1Generator = HmacGenerator<Sha1Generator>;
void GenerateHmacSha1Mac(void *dst, size_t dst_size, const void *data, size_t data_size, const void *key, size_t key_size);
void GenerateHmacSha1(void *dst, size_t dst_size, const void *data, size_t data_size, const void *key, size_t key_size);
ALWAYS_INLINE void GenerateHmacSha1Mac(void *dst, size_t dst_size, const void *data, size_t data_size, const void *key, size_t key_size) {
return GenerateHmacSha1(dst, dst_size, data, data_size, key, key_size);
}
}

View File

@@ -22,6 +22,10 @@ namespace ams::crypto {
using HmacSha256Generator = HmacGenerator<Sha256Generator>;
void GenerateHmacSha256Mac(void *dst, size_t dst_size, const void *data, size_t data_size, const void *key, size_t key_size);
void GenerateHmacSha256(void *dst, size_t dst_size, const void *data, size_t data_size, const void *key, size_t key_size);
ALWAYS_INLINE void GenerateHmacSha256Mac(void *dst, size_t dst_size, const void *data, size_t data_size, const void *key, size_t key_size) {
return GenerateHmacSha256(dst, dst_size, data, data_size, key, key_size);
}
}

View File

@@ -58,6 +58,10 @@ namespace ams::crypto {
}
};
void GenerateMd5Hash(void *dst, size_t dst_size, const void *src, size_t src_size);
void GenerateMd5(void *dst, size_t dst_size, const void *src, size_t src_size);
ALWAYS_INLINE void GenerateMd5Hash(void *dst, size_t dst_size, const void *src, size_t src_size) {
return GenerateMd5(dst, dst_size, src, src_size);
}
}

View File

@@ -58,6 +58,10 @@ namespace ams::crypto {
}
};
void GenerateSha1Hash(void *dst, size_t dst_size, const void *src, size_t src_size);
void GenerateSha1(void *dst, size_t dst_size, const void *src, size_t src_size);
ALWAYS_INLINE void GenerateSha1Hash(void *dst, size_t dst_size, const void *src, size_t src_size) {
return GenerateSha1(dst, dst_size, src, src_size);
}
}

View File

@@ -79,6 +79,10 @@ namespace ams::crypto {
}
};
void GenerateSha256Hash(void *dst, size_t dst_size, const void *src, size_t src_size);
void GenerateSha256(void *dst, size_t dst_size, const void *src, size_t src_size);
ALWAYS_INLINE void GenerateSha256Hash(void *dst, size_t dst_size, const void *src, size_t src_size) {
return GenerateSha256(dst, dst_size, src, src_size);
}
}

View File

@@ -0,0 +1,125 @@
/*
* Copyright (c) Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
#include <vapours/util.hpp>
#include <vapours/crypto/impl/crypto_sha3_impl.hpp>
namespace ams::crypto {
struct Sha3Context {
u32 hash_size;
u32 buffered_bytes;
u64 internal_state[25];
};
namespace impl {
template<size_t HashSize>
struct Sha3Asn1IdentifierByte;
template<> struct Sha3Asn1IdentifierByte<224 / BITSIZEOF(u8)> { static constexpr u8 Value = 0x07; };
template<> struct Sha3Asn1IdentifierByte<256 / BITSIZEOF(u8)> { static constexpr u8 Value = 0x08; };
template<> struct Sha3Asn1IdentifierByte<384 / BITSIZEOF(u8)> { static constexpr u8 Value = 0x09; };
template<> struct Sha3Asn1IdentifierByte<512 / BITSIZEOF(u8)> { static constexpr u8 Value = 0x0A; };
}
template <size_t _HashSize>
class Sha3Generator {
NON_COPYABLE(Sha3Generator);
NON_MOVEABLE(Sha3Generator);
private:
using Impl = impl::Sha3Impl<_HashSize>;
public:
static constexpr size_t HashSize = Impl::HashSize;
static constexpr size_t BlockSize = Impl::BlockSize;
static constexpr inline u8 Asn1Identifier[] = {
0x30, 0x31, /* Sequence, size 0x31 */
0x30, 0x0D, /* Sequence, size 0x0D */
0x06, 0x09, /* Object Identifier */
0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, impl::Sha3Asn1IdentifierByte<HashSize>::Value, /* SHA3-*** */
0x05, 0x00, /* Null */
0x04, 0x20, /* Octet string, size 0x20 */
};
static constexpr size_t Asn1IdentifierSize = util::size(Asn1Identifier);
private:
Impl m_impl;
public:
Sha3Generator() { /* ... */ }
void Initialize() {
m_impl.Initialize();
}
void Update(const void *data, size_t size) {
m_impl.Update(data, size);
}
void GetHash(void *dst, size_t size) {
m_impl.GetHash(dst, size);
}
void InitializeWithContext(const Sha3Context *context) {
m_impl.InitializeWithContext(context);
}
void GetContext(Sha3Context *context) const {
m_impl.GetContext(context);
}
};
using Sha3224Generator = Sha3Generator<224 / BITSIZEOF(u8)>;
using Sha3256Generator = Sha3Generator<256 / BITSIZEOF(u8)>;
using Sha3384Generator = Sha3Generator<384 / BITSIZEOF(u8)>;
using Sha3512Generator = Sha3Generator<512 / BITSIZEOF(u8)>;
inline void GenerateSha3224(void *dst, size_t dst_size, const void *src, size_t src_size) {
Sha3224Generator generator;
generator.Initialize();
generator.Update(src, src_size);
generator.GetHash(dst, dst_size);
}
inline void GenerateSha3256(void *dst, size_t dst_size, const void *src, size_t src_size) {
Sha3256Generator generator;
generator.Initialize();
generator.Update(src, src_size);
generator.GetHash(dst, dst_size);
}
inline void GenerateSha3384(void *dst, size_t dst_size, const void *src, size_t src_size) {
Sha3384Generator generator;
generator.Initialize();
generator.Update(src, src_size);
generator.GetHash(dst, dst_size);
}
inline void GenerateSha3512(void *dst, size_t dst_size, const void *src, size_t src_size) {
Sha3512Generator generator;
generator.Initialize();
generator.Update(src, src_size);
generator.GetHash(dst, dst_size);
}
}

View File

@@ -174,7 +174,11 @@ namespace ams::crypto::impl {
}
template<> void CtrModeImpl<AesEncryptor128>::ProcessBlocks(u8 *dst, const u8 *src, size_t num_blocks);
/* TODO: Optimized x64 CTR-192/256? */
#if defined(ATMOSPHERE_ARCH_ARM64)
template<> void CtrModeImpl<AesEncryptor192>::ProcessBlocks(u8 *dst, const u8 *src, size_t num_blocks);
template<> void CtrModeImpl<AesEncryptor256>::ProcessBlocks(u8 *dst, const u8 *src, size_t num_blocks);
#endif
}

View File

@@ -29,25 +29,30 @@ namespace ams::crypto::impl {
static constexpr size_t HashSize = 0x14;
static constexpr size_t BlockSize = 0x40;
private:
struct State {
u32 intermediate_hash[HashSize / sizeof(u32)];
u8 buffer[BlockSize];
u64 bits_consumed;
size_t num_buffered;
bool finalized;
enum State {
State_None,
State_Initialized,
State_Done,
};
private:
u32 m_intermediate_hash[HashSize / sizeof(u32)];
u8 m_buffer[BlockSize];
size_t m_buffered_bytes;
u64 m_bits_consumed;
State m_state;
public:
Sha1Impl() { m_state.finalized = false; }
Sha1Impl() : m_state(State_None) { /* ... */ }
~Sha1Impl() {
static_assert(std::is_trivially_destructible<State>::value);
ClearMemory(std::addressof(m_state), sizeof(m_state));
ClearMemory(this, sizeof(*this));
}
void Initialize();
void Update(const void *data, size_t size);
void GetHash(void *dst, size_t size);
private:
void ProcessBlock(const void *data);
void ProcessBlocks(const u8 *data, size_t block_count);
void ProcessLastBlock();
};
/* static_assert(HashFunction<Sha1Impl>); */

View File

@@ -34,20 +34,21 @@ namespace ams::crypto::impl {
static constexpr size_t HashSize = 0x20;
static constexpr size_t BlockSize = 0x40;
private:
struct State {
u32 intermediate_hash[HashSize / sizeof(u32)];
u8 buffer[BlockSize];
u64 bits_consumed;
size_t num_buffered;
bool finalized;
enum State {
State_None,
State_Initialized,
State_Done,
};
private:
u32 m_intermediate_hash[HashSize / sizeof(u32)];
u8 m_buffer[BlockSize];
size_t m_buffered_bytes;
u64 m_bits_consumed;
State m_state;
public:
Sha256Impl() { m_state.finalized = false; }
Sha256Impl() : m_state(State_None) { /* ... */ }
~Sha256Impl() {
static_assert(std::is_trivially_destructible<State>::value);
ClearMemory(std::addressof(m_state), sizeof(m_state));
ClearMemory(this, sizeof(*this));
}
void Initialize();
@@ -57,14 +58,18 @@ namespace ams::crypto::impl {
void InitializeWithContext(const Sha256Context *context);
size_t GetContext(Sha256Context *context) const;
size_t GetBufferedDataSize() const { return m_state.num_buffered; }
size_t GetBufferedDataSize() const { return m_buffered_bytes; }
void GetBufferedData(void *dst, size_t dst_size) const {
AMS_ASSERT(dst_size >= this->GetBufferedDataSize());
AMS_UNUSED(dst_size);
std::memcpy(dst, m_state.buffer, this->GetBufferedDataSize());
std::memcpy(dst, m_buffer, m_buffered_bytes);
}
private:
void ProcessBlock(const void *data);
void ProcessBlocks(const u8 *data, size_t block_count);
void ProcessLastBlock();
};
static_assert(HashFunction<Sha256Impl>);

View File

@@ -0,0 +1,70 @@
/*
* Copyright (c) Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours/common.hpp>
#include <vapours/assert.hpp>
#include <vapours/util.hpp>
#include <vapours/crypto/impl/crypto_hash_function.hpp>
#include <vapours/crypto/crypto_memory_clear.hpp>
namespace ams::crypto {
struct Sha3Context;
}
namespace ams::crypto::impl {
template<size_t _HashSize>
class Sha3Impl {
public:
static constexpr size_t InternalStateSize = 200;
static constexpr size_t HashSize = _HashSize;
static constexpr size_t BlockSize = InternalStateSize - 2 * HashSize;
private:
enum State {
State_None,
State_Initialized,
State_Done,
};
private:
u64 m_internal_state[InternalStateSize / sizeof(u64)];
size_t m_buffered_bytes;
State m_state;
public:
Sha3Impl() : m_state(State_None) { /* ... */ }
~Sha3Impl() {
ClearMemory(this, sizeof(*this));
}
void Initialize();
void Update(const void *data, size_t size);
void GetHash(void *dst, size_t size);
void InitializeWithContext(const Sha3Context *context);
void GetContext(Sha3Context *context) const;
private:
void ProcessBlock();
void ProcessLastBlock();
};
static_assert(HashFunction<Sha3Impl<224 / BITSIZEOF(u8)>>);
static_assert(HashFunction<Sha3Impl<256 / BITSIZEOF(u8)>>);
static_assert(HashFunction<Sha3Impl<384 / BITSIZEOF(u8)>>);
static_assert(HashFunction<Sha3Impl<512 / BITSIZEOF(u8)>>);
}

View File

@@ -125,6 +125,7 @@ namespace ams::crypto::impl {
size_t ProcessRemainingData(u8 *dst, const u8 *src, size_t size);
};
#if defined(ATMOSPHERE_ARCH_ARM64)
template<> size_t XtsModeImpl::Update<AesEncryptor128>(void *dst, size_t dst_size, const void *src, size_t src_size);
template<> size_t XtsModeImpl::Update<AesEncryptor192>(void *dst, size_t dst_size, const void *src, size_t src_size);
template<> size_t XtsModeImpl::Update<AesEncryptor256>(void *dst, size_t dst_size, const void *src, size_t src_size);
@@ -132,5 +133,6 @@ namespace ams::crypto::impl {
template<> size_t XtsModeImpl::Update<AesDecryptor128>(void *dst, size_t dst_size, const void *src, size_t src_size);
template<> size_t XtsModeImpl::Update<AesDecryptor192>(void *dst, size_t dst_size, const void *src, size_t src_size);
template<> size_t XtsModeImpl::Update<AesDecryptor256>(void *dst, size_t dst_size, const void *src, size_t src_size);
#endif
}