[*] API design change, MemoryViews must be passed by reference such that a pointer to an object that may be the superclass of the actual RAII owner of the memory is passed

This commit is contained in:
Reece Wilson 2021-09-15 19:55:22 +01:00
parent 2c68cf529a
commit 412d798994
28 changed files with 87 additions and 91 deletions

View File

@ -9,7 +9,7 @@
namespace Aurora::Crypto::AES
{
AUKN_SYM AuUInt GetSafeCipherPadding(Memory::MemoryViewRead plainText);
AUKN_SYM AuUInt GetSafeCipherPadding(const Memory::MemoryViewRead & plainText);
// Remember: AES works in chunks of 128 bits
// IVS are 16 bytes long
@ -20,17 +20,17 @@ namespace Aurora::Crypto::AES
// It is not the end of the world if an IV is made public by design
// Keys must be random
// Initialization vectors could be derived from SHA1, Tiger, or SHA2 digests
AUKN_SYM bool Encrypt(Memory::MemoryViewRead plainText,
Memory::MemoryViewRead inIv,
Memory::MemoryViewWrite outIv,
Memory::MemoryViewRead inKey,
AUKN_SYM bool Encrypt(const Memory::MemoryViewRead & plainText,
const Memory::MemoryViewRead & inIv,
const Memory::MemoryViewWrite & outIv,
const Memory::MemoryViewRead & inKey,
AuList<AuUInt8> &out,
bool auCoolCodePadding);
AUKN_SYM bool Decrypt(Memory::MemoryViewRead cipherText,
Memory::MemoryViewRead inIv,
Memory::MemoryViewWrite outIv,
Memory::MemoryViewRead inKey,
AUKN_SYM bool Decrypt(const Memory::MemoryViewRead & cipherText,
const Memory::MemoryViewRead & inIv,
const Memory::MemoryViewWrite & outIv,
const Memory::MemoryViewRead & inKey,
AuList<AuUInt8>& plainText,
bool auCoolCodePadding);
}

View File

@ -9,46 +9,42 @@
namespace Aurora::Crypto::ECC
{
#if 0
class IECCPublic
{
public:
virtual bool Verify(const void *hash, AuUInt hashLength,
const void *sigBuffer, AuUInt sigLength) = 0;
virtual bool Verify(const void *buffer, AuUInt length,
const void *sigBuffer, AuUInt sigLength,
EHashType method) = 0;
virtual bool Verify(const AuList<AuUInt8> &buffer, const AuList<AuUInt8> &sig,
EHashType method) = 0;
virtual bool Verify(const Memory::MemoryViewRead &payload,
const Memory::MemoryViewRead &signature) = 0;
virtual bool AsPublicECC(PublicECCKey &out) = 0;
virtual bool AsPublicECC(AuList<AuUInt8> &out) = 0;
};
class IECCPrivate
{
public:
virtual bool Sign(const void *buffer, AuUInt length,
virtual bool Sign(const Memory::MemoryViewRead &payload,
EHashType method,
AuList<AuUInt8> &out) = 0;
virtual bool Sign(const AuList<AuUInt8> &in,
EHashType method,
AuList<AuUInt8> &out) = 0;
virtual bool ECDH(const AuSPtr<IECCPublic> &partnerPublic,
AuList<AuUInt8> &sharedKey) = 0;
virtual bool Sign(const void *hash, AuUInt hashLength, AuList<AuUInt8> &out) = 0;
virtual bool ECDH(IECCPublic *partnerPublic, AuList<AuUInt8> &sharedKey) = 0;
virtual bool AsPublicECC(PublicECCKey &out) = 0;
virtual bool AsPrivateECC(PrivateECCKey &out) = 0;
virtual bool AsPublicECC(AuList<AuUInt8> &out) = 0;
virtual bool AsPrivateECC(AuList<AuUInt8> &out) = 0;
};
#endif
enum EECCCurve
{
eCurve256,
eCurve384,
eCurveX25519,
eCurveEd25519
};
AUKN_SHARED_API(NewECC, IECCPrivate, EECCCurve curve);
AUKN_SHARED_API(OpenPrivateECC, IECCPrivate, EECCCurve curve, const Memory::MemoryViewRead &pk);
AUKN_SHARED_API(OpenPublicECC, IECCPublic, EECCCurve curve, const Memory::MemoryViewRead &pk);
AUKN_SHARED_API(OpenPublicECCFromCert, IECCPublic, EECCCurve curve, const Memory::MemoryViewRead &certificate);
}
#include "25519/25519.hpp"

View File

@ -13,12 +13,12 @@ namespace Aurora::Crypto::RSA
class IRSAPrivate
{
public:
virtual bool Sign(Memory::MemoryViewRead payload,
virtual bool Sign(const Memory::MemoryViewRead & payload,
EHashType method,
EPaddingType type,
AuList<AuUInt8> &out) = 0;
virtual bool Decrypt(Memory::MemoryViewRead payload,
virtual bool Decrypt(const Memory::MemoryViewRead & payload,
EPaddingType type,
AuList<AuUInt8> &out) = 0;

View File

@ -13,12 +13,12 @@ namespace Aurora::Crypto::RSA
class IRSAPublic
{
public:
virtual bool Verify(Memory::MemoryViewRead payload,
Memory::MemoryViewRead signature,
virtual bool Verify(const Memory::MemoryViewRead & payload,
const Memory::MemoryViewRead & signature,
EHashType method,
EPaddingType type) = 0;
virtual bool Encrypt(Memory::MemoryViewRead plainText,
virtual bool Encrypt(const Memory::MemoryViewRead & plainText,
EPaddingType type,
AuList<AuUInt8> &out) = 0;

View File

@ -23,7 +23,7 @@ namespace Aurora::IO::Buffered
return EStreamError::eErrorNone;
}
virtual EStreamError ArbitraryRead(AuUInt32 offset, Memory::MemoryViewStreamWrite paramters) override
virtual EStreamError ArbitraryRead(AuUInt32 offset, const Memory::MemoryViewStreamWrite &paramters) override
{
if (buffer_.empty()) return EStreamError::eErrorEndOfStream;

View File

@ -23,7 +23,7 @@ namespace Aurora::IO::Buffered
return EStreamError::eErrorNone;
}
virtual EStreamError Read(Memory::MemoryViewStreamWrite paramters) override
virtual EStreamError Read(const Memory::MemoryViewStreamWrite &paramters) override
{
auto realEndOffset = std::min(buffer_.size() - offset_, paramters.length);
if (realEndOffset == 0) return EStreamError::eErrorEndOfStream;

View File

@ -22,7 +22,7 @@ namespace Aurora::IO::Buffered
return EStreamError::eErrorNone;
}
virtual EStreamError Write(Memory::MemoryViewStreamRead parameters) override
virtual EStreamError Write(const Memory::MemoryViewStreamRead & parameters) override
{
auto idx = buffer_.size();
buffer_.resize(idx + parameters.length);

View File

@ -29,7 +29,7 @@ namespace Aurora::IO::FS
return stream_ ? EStreamError::eErrorNone : EStreamError::eErrorStreamInterrupted;
}
virtual EStreamError ArbitraryRead(AuUInt32 offset, Memory::MemoryViewStreamWrite paramters) override
virtual EStreamError ArbitraryRead(AuUInt32 offset, const Memory::MemoryViewStreamWrite &paramters) override
{
if (!stream_) return EStreamError::eErrorStreamNotOpen;
if (!stream_->SetOffset(offset)) return EStreamError::eErrorEndOfStream;

View File

@ -29,7 +29,7 @@ namespace Aurora::IO::FS
return stream_ ? EStreamError::eErrorNone : EStreamError::eErrorStreamInterrupted;
}
virtual EStreamError Read(Memory::MemoryViewStreamWrite paramters) override
virtual EStreamError Read(const Memory::MemoryViewStreamWrite &paramters) override
{
if (!stream_) return EStreamError::eErrorStreamNotOpen;
if (!stream_->Read(paramters)) return EStreamError::eErrorStreamInterrupted;

View File

@ -15,8 +15,8 @@ namespace Aurora::IO::FS
virtual AuUInt64 GetOffset() = 0;
virtual bool SetOffset(AuUInt64 offset) = 0;
virtual AuUInt64 GetLength() = 0;
virtual bool Read(Memory::MemoryViewStreamWrite parameters) = 0;
virtual bool Write(Memory::MemoryViewStreamRead parameters) = 0;
virtual bool Read(const Memory::MemoryViewStreamWrite &parameters) = 0;
virtual bool Write(const Memory::MemoryViewStreamRead &parameters) = 0;
virtual void Close() = 0;
virtual void Flush() = 0;
};

View File

@ -29,7 +29,7 @@ namespace Aurora::IO::FS
return stream_ ? EStreamError::eErrorNone : EStreamError::eErrorStreamInterrupted;
}
virtual EStreamError Write(Memory::MemoryViewStreamRead parameters) override
virtual EStreamError Write(const Memory::MemoryViewStreamRead & parameters) override
{
if (!stream_) return EStreamError::eErrorStreamNotOpen;
if (!stream_->Write(parameters)) return EStreamError::eErrorStreamInterrupted;

View File

@ -13,7 +13,7 @@ namespace Aurora::IO
{
public:
virtual EStreamError Open() = 0;
virtual EStreamError ArbitraryRead(AuUInt32 offset, Memory::MemoryViewStreamWrite paramters) = 0;
virtual EStreamError ArbitraryRead(AuUInt32 offset, const Memory::MemoryViewStreamWrite &paramters) = 0;
virtual void Close() = 0;
};
}

View File

@ -13,7 +13,7 @@ namespace Aurora::IO
{
public:
virtual EStreamError Open() = 0;
virtual EStreamError Read(Memory::MemoryViewStreamWrite paramters) = 0;
virtual EStreamError Read(const Memory::MemoryViewStreamWrite &paramters) = 0;
virtual void Close() = 0;
EStreamError ReadAll(AuList<AuUInt8> &buffer)

View File

@ -13,7 +13,7 @@ namespace Aurora::IO
{
public:
virtual EStreamError Open() = 0;
virtual EStreamError Write(Memory::MemoryViewStreamRead parameters) = 0;
virtual EStreamError Write(const Memory::MemoryViewStreamRead & parameters) = 0;
virtual void Flush() = 0;
virtual void Close() = 0;
};

View File

@ -9,17 +9,17 @@
namespace Aurora::Locale::Encoding
{
AUKN_SYM AuStreamReadWrittenPair_t EncodeUTF8(Aurora::Memory::MemoryViewRead utf8, Aurora::Memory::MemoryViewWrite binary, ECodePage page = ECodePage::eUnsupported);
static inline AuStreamReadWrittenPair_t EncodeUTF8(const AuString &utf8, Aurora::Memory::MemoryViewWrite binary, ECodePage page = ECodePage::eUnsupported)
AUKN_SYM AuStreamReadWrittenPair_t EncodeUTF8(const Aurora::Memory::MemoryViewRead & utf8, const Aurora::Memory::MemoryViewWrite & binary, ECodePage page = ECodePage::eUnsupported);
static inline AuStreamReadWrittenPair_t EncodeUTF8(const AuString &utf8, const Aurora::Memory::MemoryViewWrite & binary, ECodePage page = ECodePage::eUnsupported)
{
return EncodeUTF8(Aurora::Memory::MemoryViewRead(utf8), binary, page);
}
AUKN_SYM std::optional<AuPair<ECodePage, AuUInt8>> DecodeBOM(Aurora::Memory::MemoryViewRead binary);
AUKN_SYM std::optional<AuPair<ECodePage, AuUInt8>> DecodeBOM(const Aurora::Memory::MemoryViewRead & binary);
/// Translates a buffer, possibly a slice of a stream, to UTF-8
/// Returns a pair; bytes consumed, bytes written
AUKN_SYM AuStreamReadWrittenPair_t DecodeUTF8(Aurora::Memory::MemoryViewRead binary, Aurora::Memory::MemoryViewWrite utf8, ECodePage page = ECodePage::eUnsupported);
AUKN_SYM AuStreamReadWrittenPair_t DecodeUTF8(const Aurora::Memory::MemoryViewRead & binary, const Aurora::Memory::MemoryViewWrite & utf8, ECodePage page = ECodePage::eUnsupported);
AUKN_SYM AuStreamReadWrittenPair_t DecodeUTF8(Aurora::Memory::MemoryViewRead binary, AuString &out, ECodePage page = ECodePage::eUnsupported);
AUKN_SYM AuStreamReadWrittenPair_t DecodeUTF8(const Aurora::Memory::MemoryViewRead & binary, AuString &out, ECodePage page = ECodePage::eUnsupported);
}

View File

@ -158,6 +158,7 @@ namespace Aurora::Memory
}
AuUInt &outVariable;
private:
AuUInt unused;
};

View File

@ -104,7 +104,6 @@ namespace Aurora::Console::ConsoleFIO
if (gFileHandle)
{
AuUInt idc;
gFileHandle->Write(Aurora::Memory::MemoryViewStreamRead(gLogBuffer));
}

View File

@ -13,7 +13,7 @@
namespace Aurora::Crypto::AES
{
AUKN_SYM AuUInt GetSafeCipherPadding(Memory::MemoryViewRead parameters)
AUKN_SYM AuUInt GetSafeCipherPadding(const Memory::MemoryViewRead & parameters)
{
auto tptr = reinterpret_cast<const unsigned char *>(parameters.ptr);
@ -139,10 +139,10 @@ namespace Aurora::Crypto::AES
return true;
}
AUKN_SYM bool Encrypt(Memory::MemoryViewRead plainText,
Memory::MemoryViewRead inIv,
Memory::MemoryViewWrite outIv,
Memory::MemoryViewRead inKey,
AUKN_SYM bool Encrypt(const Memory::MemoryViewRead & plainText,
const Memory::MemoryViewRead & inIv,
const Memory::MemoryViewWrite & outIv,
const Memory::MemoryViewRead & inKey,
AuList<AuUInt8> &out,
bool auCoolCodePadding)
{
@ -254,10 +254,10 @@ namespace Aurora::Crypto::AES
}
AUKN_SYM bool Decrypt(Memory::MemoryViewRead cipherText,
Memory::MemoryViewRead inIv,
Memory::MemoryViewWrite outIv,
Memory::MemoryViewRead inKey,
AUKN_SYM bool Decrypt(const Memory::MemoryViewRead & cipherText,
const Memory::MemoryViewRead & inIv,
const Memory::MemoryViewWrite & outIv,
const Memory::MemoryViewRead & inKey,
AuList<AuUInt8> &plainText,
bool safe)
{

View File

@ -23,7 +23,7 @@ namespace Aurora::Crypto::RSA
rsa_free(&key_);
}
bool PrivateRSA::Sign(Memory::MemoryViewRead payload,
bool PrivateRSA::Sign(const Memory::MemoryViewRead & payload,
EHashType method,
EPaddingType type,
AuList<AuUInt8> &out)
@ -88,7 +88,7 @@ namespace Aurora::Crypto::RSA
return true;
}
bool PrivateRSA::Decrypt(Memory::MemoryViewRead payload,
bool PrivateRSA::Decrypt(const Memory::MemoryViewRead & payload,
EPaddingType type,
AuList<AuUInt8> &out)
{

View File

@ -15,12 +15,12 @@ namespace Aurora::Crypto::RSA
PrivateRSA(rsa_key &key);
~PrivateRSA();
bool Sign(Memory::MemoryViewRead payload,
bool Sign(const Memory::MemoryViewRead & payload,
EHashType method,
EPaddingType type,
AuList<AuUInt8> &out) override;
bool Decrypt(Memory::MemoryViewRead payload,
bool Decrypt(const Memory::MemoryViewRead & payload,
EPaddingType type,
AuList<AuUInt8> &out) override;

View File

@ -25,8 +25,8 @@ namespace Aurora::Crypto::RSA
}
}
bool PublicRSA::Verify(Memory::MemoryViewRead payload,
Memory::MemoryViewRead signature,
bool PublicRSA::Verify(const Memory::MemoryViewRead & payload,
const Memory::MemoryViewRead & signature,
EHashType method,
EPaddingType type)
{
@ -75,7 +75,7 @@ namespace Aurora::Crypto::RSA
return ok == 1;
}
bool PublicRSA::Encrypt(Memory::MemoryViewRead plainText,
bool PublicRSA::Encrypt(const Memory::MemoryViewRead & plainText,
EPaddingType type,
AuList<AuUInt8> &out)
{

View File

@ -16,12 +16,12 @@ namespace Aurora::Crypto::RSA
PublicRSA(rsa_key &key, bool owned = true);
~PublicRSA();
bool Verify(Memory::MemoryViewRead payload,
Memory::MemoryViewRead signature,
bool Verify(const Memory::MemoryViewRead & payload,
const Memory::MemoryViewRead & signature,
EHashType method,
EPaddingType type) override;
bool Encrypt(Memory::MemoryViewRead plainText,
bool Encrypt(const Memory::MemoryViewRead & plainText,
EPaddingType type,
AuList<AuUInt8> &out) override;

View File

@ -230,8 +230,8 @@ namespace Aurora::IO::FS
}
bool ok {};
ok = stream->Write(Memory::MemoryViewStreamRead{bom});
ok &= stream->Write(Memory::MemoryViewStreamRead{str});
ok = stream->Write(const Memory::MemoryViewStreamRead &{bom});
ok &= stream->Write(const Memory::MemoryViewStreamRead &{str});
stream->Flush();
return ok;

View File

@ -100,7 +100,7 @@ namespace Aurora::IO::FS
return true;
}
bool Read(Memory::MemoryViewStreamWrite parameters) override
bool Read(const Memory::MemoryViewStreamWrite &parameters) override
{
try
{
@ -119,7 +119,7 @@ namespace Aurora::IO::FS
}
}
bool Write(Memory::MemoryViewStreamRead parameters) override
bool Write(const Memory::MemoryViewStreamRead & parameters) override
{
return false;
}
@ -134,12 +134,12 @@ namespace Aurora::IO::FS
offset_ = stream_.tellp();
}
bool Read(Memory::MemoryViewStreamWrite parameters) override
bool Read(const Memory::MemoryViewStreamWrite &parameters) override
{
return false;
}
bool Write(Memory::MemoryViewStreamRead parameters) override
bool Write(const Memory::MemoryViewStreamRead & parameters) override
{
try
{

View File

@ -89,7 +89,7 @@ namespace Aurora::IO::FS
return length.QuadPart;
}
bool WinFileStream::Read(Memory::MemoryViewStreamWrite parameters)
bool WinFileStream::Read(const Memory::MemoryViewStreamWrite &parameters)
{
if (handle_ == INVALID_HANDLE_VALUE)
{
@ -130,7 +130,7 @@ namespace Aurora::IO::FS
return true;
}
bool WinFileStream::Write(Memory::MemoryViewStreamRead parameters)
bool WinFileStream::Write(const Memory::MemoryViewStreamRead & parameters)
{
if (handle_ == INVALID_HANDLE_VALUE)
{

View File

@ -21,8 +21,8 @@ namespace Aurora::IO::FS
AuUInt64 GetOffset() override;
bool SetOffset(AuUInt64 offset) override;
AuUInt64 GetLength() override;
bool Read(Memory::MemoryViewStreamWrite parameters) override;
bool Write(Memory::MemoryViewStreamRead parameters) override;
bool Read(const Memory::MemoryViewStreamWrite &parameters) override;
bool Write(const Memory::MemoryViewStreamRead & parameters) override;
void Close() override;
void Flush() override;

View File

@ -161,7 +161,7 @@ namespace Aurora::IO::FS
return PosixGetLength(handle_);
}
bool PosixFileStream::Read(Memory::MemoryViewStreamWrite parameters)
bool PosixFileStream::Read(const Memory::MemoryViewStreamWrite &parameters)
{
if (handle_ == -1)
{
@ -201,7 +201,7 @@ namespace Aurora::IO::FS
return true;
}
bool PosixFileStream::Write(Memory::MemoryViewStreamRead parameters)
bool PosixFileStream::Write(const Memory::MemoryViewStreamRead &parameters)
{
if (handle_ == -1)
{

View File

@ -11,7 +11,7 @@
namespace Aurora::Locale::Encoding
{
AUKN_SYM AuOptional<AuPair<ECodePage, AuUInt8>> DecodeBOM(Aurora::Memory::MemoryViewRead binary)
AUKN_SYM AuOptional<AuPair<ECodePage, AuUInt8>> DecodeBOM(const Aurora::Memory::MemoryViewRead & binary)
{
#define ADD_PATTERN(str, code) {str, AuArraySize(str) - 1, ECodePage::code}
AuList<std::tuple<const char *, int, ECodePage>> bows =
@ -41,19 +41,19 @@ namespace Aurora::Locale::Encoding
return {};
}
AUKN_SYM AuStreamReadWrittenPair_t EncodeUTF8(Aurora::Memory::MemoryViewRead utf8, Aurora::Memory::MemoryViewWrite binary, ECodePage page)
AUKN_SYM AuStreamReadWrittenPair_t EncodeUTF8(const Aurora::Memory::MemoryViewRead & utf8, const Aurora::Memory::MemoryViewWrite & binary, ECodePage page)
{
TextStreamEncoder re(page);
return re.DecodeUTF8(utf8.ptr, utf8.length, binary.ptr, binary.length);
}
AUKN_SYM AuStreamReadWrittenPair_t DecodeUTF8(Aurora::Memory::MemoryViewRead binary, Aurora::Memory::MemoryViewWrite utf8, ECodePage page)
AUKN_SYM AuStreamReadWrittenPair_t DecodeUTF8(const Aurora::Memory::MemoryViewRead & binary, const Aurora::Memory::MemoryViewWrite & utf8, ECodePage page)
{
TextStreamProcessor re(page);
return re.EncodeUTF8(binary.ptr, binary.length, utf8.ptr, utf8.length);
}
AUKN_SYM AuStreamReadWrittenPair_t DecodeUTF8(Aurora::Memory::MemoryViewRead binary, AuString &out, ECodePage page)
AUKN_SYM AuStreamReadWrittenPair_t DecodeUTF8(const Aurora::Memory::MemoryViewRead & binary, AuString &out, ECodePage page)
{
auto aaa = DecodeUTF8(binary, {}, page);
out.resize(aaa.second);