From 9bd05a07523e9b3bce37b894c475f71da3bd8a88 Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Sat, 26 Aug 2023 21:31:34 +0100 Subject: [PATCH] [+] HashStream SOO [+] HashSteam extension padding for future use --- Include/Aurora/Hashing/HashStream.hpp | 6 ++++- Source/Hashing/AuHashStream.cpp | 36 ++++++++++++++++++--------- Source/Hashing/AuHashStream.hpp | 15 ++++++++--- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/Include/Aurora/Hashing/HashStream.hpp b/Include/Aurora/Hashing/HashStream.hpp index ceaad725..ef85c2fc 100644 --- a/Include/Aurora/Hashing/HashStream.hpp +++ b/Include/Aurora/Hashing/HashStream.hpp @@ -9,6 +9,8 @@ namespace Aurora::Hashing { + static const auto kSizeHashStream = 512ul; + struct IHashStream { /** @@ -58,5 +60,7 @@ namespace Aurora::Hashing virtual void Reset() = 0; }; - AUKN_SHARED_API(HashStream, IHashStream, EHashType type); + AUKN_SHARED_SOO2(HashStream, IHashStream, kSizeHashStream, + ((EHashType,type)), + EHashType type); } \ No newline at end of file diff --git a/Source/Hashing/AuHashStream.cpp b/Source/Hashing/AuHashStream.cpp index 525c480b..ead7ab47 100644 --- a/Source/Hashing/AuHashStream.cpp +++ b/Source/Hashing/AuHashStream.cpp @@ -13,12 +13,17 @@ namespace Aurora::Hashing { #define DIGEST_CHECK(n) SysAssert(n == CRYPT_OK) - HashStream::HashStream(EHashType type) : type_(type) + HashStreamImpl::HashStreamImpl(EHashType type) : type_(type) { + if (!EHashTypeIsValid(type)) + { + AU_THROW_CONST_STRING("Invalid Hash Type"); + } + Init(); } - void HashStream::Ingest(const Memory::MemoryViewRead &input) + void HashStreamImpl::Ingest(const Memory::MemoryViewRead &input) { if (this->bFinished_) { @@ -72,7 +77,7 @@ namespace Aurora::Hashing } } - AuUInt8 const* HashStream::GetBytes(AuUInt32 &length) + AuUInt8 const* HashStreamImpl::GetBytes(AuUInt32 &length) { switch (this->type_) { @@ -201,14 +206,14 @@ namespace Aurora::Hashing return nullptr; } - AuMemoryViewRead HashStream::Finalize() + AuMemoryViewRead HashStreamImpl::Finalize() { AuUInt32 length; auto begin = GetBytes(length); return AuMemoryViewRead(begin, length); } - Memory::MemoryViewRead HashStream::PeekFinalize() + Memory::MemoryViewRead HashStreamImpl::PeekFinalize() { if (this->bFinished_) { @@ -224,7 +229,7 @@ namespace Aurora::Hashing return view; } - AuResult HashStream::Export() + AuResult HashStreamImpl::Export() { // Defer to HASH_PROCESS defined in the private libtomcrypt header @@ -289,7 +294,7 @@ namespace Aurora::Hashing #undef ADD_EXPORT } - bool HashStream::Import(const Memory::MemoryViewRead &view) + bool HashStreamImpl::Import(const Memory::MemoryViewRead &view) { this->bFinished_ = false; @@ -363,12 +368,12 @@ namespace Aurora::Hashing #undef ADD_IMPORT } - void HashStream::Reset() + void HashStreamImpl::Reset() { Init(); } - void HashStream::Init() + void HashStreamImpl::Init() { this->bFinished_ = false; AuMemset(&this->state_, 0, sizeof(this->state_)); @@ -425,11 +430,18 @@ namespace Aurora::Hashing AUKN_SYM IHashStream *HashStreamNew(EHashType type) { - return _new HashStream(type); + if (!EHashTypeIsValid(type)) + { + return {}; + } + + return _new HashStreamImpl(type); } - AUKN_SYM void HashStreamRelease(IHashStream *stream) + AUKN_SYM void HashStreamRelease(IHashStream *pStream) { - AuSafeDelete(stream); + AuSafeDelete(pStream); } + + AUROXTL_INTERFACE_SOO_SRC(HashStream, HashStreamImpl, (EHashType, type)) } \ No newline at end of file diff --git a/Source/Hashing/AuHashStream.hpp b/Source/Hashing/AuHashStream.hpp index d49debff..284c0aaf 100644 --- a/Source/Hashing/AuHashStream.hpp +++ b/Source/Hashing/AuHashStream.hpp @@ -9,9 +9,11 @@ namespace Aurora::Hashing { - struct HashStream : IHashStream +#pragma pack(push) +#pragma pack(4) + struct HashStreamImpl : IHashStream { - HashStream(EHashType type); + HashStreamImpl(EHashType type); void Ingest(const Memory::MemoryViewRead &input) override; @@ -28,9 +30,14 @@ namespace Aurora::Hashing void Init(); private: - AuUInt8 buffer_[64] {}; - hash_state state_ {}; EHashType type_ {}; bool bFinished_ {}; + AuUInt8 buffer_[64] {}; + union + { + hash_state state_ {}; + char altBuffer_[432]; + }; }; +#pragma pack(pop) } \ No newline at end of file