ams: support building unit test programs on windows/linux/macos
This commit is contained in:
@@ -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
|
||||
|
||||
}
|
||||
|
||||
@@ -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>); */
|
||||
|
||||
@@ -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>);
|
||||
|
||||
@@ -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)>>);
|
||||
|
||||
}
|
||||
@@ -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
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user