[*] transition some of the old vector based AuByteBuffer APIs to be stream aware

[*] fix: runnersRunning was uninitialized
[*] fix: unimplemented path in GetOrAllocateLinearWriteable
This commit is contained in:
Reece Wilson 2022-09-19 06:42:39 +01:00
parent 8a2947ffc5
commit 050e938f0c
11 changed files with 202 additions and 213 deletions

View File

@ -270,6 +270,10 @@ namespace Aurora::Memory
return this->GetLinearWriteable(length);
}
else
{
return {};
}
}
AuUInt ByteBuffer::GetReadOffset() const

View File

@ -129,6 +129,6 @@ namespace Aurora::Async
bool shuttingdown_ {};
AuThreadPrimitives::RWLockUnique_t rwlock_;
std::atomic_int tasksRunning_;
bool runnersRunning_;
bool runnersRunning_ {};
};
}

View File

@ -103,26 +103,26 @@ namespace Aurora::Crypto::ECC
bool ExportECCKey(const ecc_key &key, bool pub, DerBuffer &out)
{
if (!AuTryResize(out, 4096))
{
return false;
}
unsigned long actualSize = out.size();
auto ret = ecc_export_openssl(out.data(), &actualSize, pub ? PK_PUBLIC : PK_PRIVATE, &key);
if (ret != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
return false;
}
if (!AuTryResize(out, actualSize))
auto writeView = out.GetOrAllocateLinearWriteable(4096);
if (!writeView)
{
SysPushErrorMem();
return {};
}
unsigned long actualSize = 4096;
auto iRet = ::ecc_export_openssl(out.writePtr,
&actualSize, pub ? PK_PUBLIC : PK_PRIVATE,
&key);
if (iRet != CRYPT_OK)
{
SysPushErrorCrypt("{}", iRet);
return false;
}
out.writePtr +=actualSize;
return true;
}

View File

@ -46,12 +46,6 @@ namespace Aurora::Crypto::ECC
return false;
}
if (!AuTryResize(out, 1024))
{
SysPushErrorMem();
return false;
}
AuByteBuffer hashVec;
if (!AuTryResize(hashVec, 128))
{
@ -59,16 +53,16 @@ namespace Aurora::Crypto::ECC
return false;
}
unsigned long hashSize = hashVec.size();
auto ret = hash_memory(hash,
reinterpret_cast<const unsigned char *>(plainText.ptr), plainText.length,
reinterpret_cast<unsigned char *>(hashVec.data()), &hashSize);
if (ret != CRYPT_OK)
unsigned long hashSize = 1024;
auto iRet = ::hash_memory(hash,
reinterpret_cast<const unsigned char *>(plainText.ptr), plainText.length,
reinterpret_cast<unsigned char *>(hashVec.data()), &hashSize);
if (iRet != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
SysPushErrorCrypt("{}", iRet);
return false;
}
return Sign(hashVec, out);
}
@ -86,28 +80,25 @@ namespace Aurora::Crypto::ECC
return {};
}
if (!AuTryResize(out, 1024))
auto writeView = out.GetOrAllocateLinearWriteable(1024);
if (!writeView)
{
SysPushErrorMem();
return false;
return {};
}
unsigned long len = out.size();
unsigned long len = 1024;
auto ret = ed25519_sign(reinterpret_cast<const unsigned char *>(hash.ptr), hash.length,
out.data(), &len,
&key_);
if (ret != CRYPT_OK)
auto iRet = ::ed25519_sign(AuReinterpretCast<const unsigned char *>(hash.ptr), hash.length,
out.data(), &len,
&key_);
if (iRet != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
SysPushErrorCrypt("{}", iRet);
return false;
}
if (!AuTryResize(out, len))
{
SysPushErrorMem();
return false;
}
out.writePtr += len;
return true;
}
@ -120,12 +111,11 @@ namespace Aurora::Crypto::ECC
return false;
}
AuByteBuffer sharedSecret;
if (!AuTryResize(sharedSecret, 128))
auto writeView = sharedKey.GetOrAllocateLinearWriteable(128);
if (!writeView)
{
SysPushErrorMem();
return false;
return {};
}
if (partnerPublic->GetType() == this->GetType())
@ -134,42 +124,42 @@ namespace Aurora::Crypto::ECC
return false;
}
unsigned long actualSize = sharedSecret.size();
auto ret = x25519_shared_secret(&key_, &(AuReinterpretCast<PublicCurve25519Impl>(partnerPublic)->GetKey()), sharedSecret.data(), &actualSize);
if (ret != CRYPT_OK)
unsigned long actualSize = 128;
auto iRet = ::x25519_shared_secret(&this->key_,
&(AuReinterpretCast<PublicCurve25519Impl>(partnerPublic)->GetKey()),
sharedKey.writePtr,
&actualSize);
if (iRet != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
return false;
}
if (!AuTryResize(sharedSecret, actualSize))
{
SysPushErrorMem();
SysPushErrorCrypt("{}", iRet);
return false;
}
sharedKey.writePtr += actualSize;
return true;
}
bool PrivateCurve25519Impl::AsPublicECC(Memory::ByteBuffer &out)
{
if (!AuTryResize(out, 1024))
auto writeView = out.GetOrAllocateLinearWriteable(4096);
if (!writeView)
{
return false;
SysPushErrorMem();
return {};
}
unsigned long actualSize;
int ret;
actualSize = 4096;
if (this->isX25519_)
{
actualSize = out.size();
ret = x25519_export(out.data(), &actualSize, PK_PUBLIC, &this->key_);
ret = x25519_export(out.writePtr, &actualSize, PK_PUBLIC, &this->key_);
}
else
{
actualSize = out.size();
ret = ed25519_export(out.data(), &actualSize, PK_PUBLIC, &this->key_);
ret = ed25519_export(out.writePtr, &actualSize, PK_PUBLIC, &this->key_);
}
if (ret != CRYPT_OK)
@ -178,34 +168,32 @@ namespace Aurora::Crypto::ECC
return false;
}
if (!AuTryResize(out, actualSize))
{
SysPushErrorMem();
return false;
}
out.writePtr += actualSize;
return true;
}
bool PrivateCurve25519Impl::AsPrivateECC(Memory::ByteBuffer &out)
{
if (!AuTryResize(out, 1024))
auto writeView = out.GetOrAllocateLinearWriteable(4096);
if (!writeView)
{
return false;
SysPushErrorMem();
return {};
}
unsigned long actualSize;
int ret;
actualSize = 4096;
if (this->isX25519_)
{
actualSize = out.size();
ret = x25519_export(out.data(), &actualSize, PK_PRIVATE, &this->key_);
ret = x25519_export(out.writePtr, &actualSize, PK_PRIVATE, &this->key_);
}
else
{
actualSize = out.size();
ret = ed25519_export(out.data(), &actualSize, PK_PRIVATE, &this->key_);
ret = ed25519_export(out.writePtr, &actualSize, PK_PRIVATE, &this->key_);
}
if (ret != CRYPT_OK)
@ -214,11 +202,7 @@ namespace Aurora::Crypto::ECC
return false;
}
if (!AuTryResize(out, actualSize))
{
SysPushErrorMem();
return false;
}
out.writePtr += actualSize;
return true;
}

View File

@ -104,23 +104,25 @@ namespace Aurora::Crypto::ECC
bool PublicCurve25519Impl::AsPublicECC(Memory::ByteBuffer &out)
{
if (!AuTryResize(out, 4096))
auto writeView = out.GetOrAllocateLinearWriteable(4096);
if (!writeView)
{
return false;
SysPushErrorMem();
return {};
}
unsigned long actualSize;
int ret;
actualSize = 4096;
if (this->isX25519_)
{
actualSize = out.size();
ret = x25519_export(out.data(), &actualSize, PK_PUBLIC, &this->key_);
ret = ::x25519_export(out.writePtr, &actualSize, PK_PUBLIC, &this->key_);
}
else
{
actualSize = out.size();
ret = ed25519_export(out.data(), &actualSize, PK_PUBLIC, &this->key_);
ret = ::ed25519_export(out.writePtr, &actualSize, PK_PUBLIC, &this->key_);
}
if (ret != CRYPT_OK)
@ -129,11 +131,7 @@ namespace Aurora::Crypto::ECC
return false;
}
if (!AuTryResize(out, actualSize))
{
SysPushErrorMem();
return false;
}
out.writePtr += actualSize;
return true;
}

View File

@ -21,12 +21,12 @@ namespace Aurora::Crypto::ECC
PrivateECCImpl::~PrivateECCImpl()
{
ecc_free(&_key);
ecc_free(&this->_key);
}
EECCCurve PrivateECCImpl::GetType()
{
return _type;
return this->_type;
}
bool PrivateECCImpl::Sign(const AuMemoryViewRead &plainText,
@ -63,9 +63,9 @@ namespace Aurora::Crypto::ECC
}
unsigned long hashSize = hashVec.size();
auto ret = hash_memory(hash,
reinterpret_cast<const unsigned char *>(plainText.ptr), plainText.length,
reinterpret_cast<unsigned char *>(hashVec.data()), &hashSize);
auto ret = ::hash_memory(hash,
AuReinterpretCast<const unsigned char *>(plainText.ptr), plainText.length,
AuReinterpretCast<unsigned char *>(hashVec.data()), &hashSize);
if (ret != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
@ -85,32 +85,29 @@ namespace Aurora::Crypto::ECC
SysPushErrorParam();
return {};
}
if (!AuTryResize(out, 1024))
auto writeView = out.GetOrAllocateLinearWriteable(1024);
if (!writeView)
{
SysPushErrorMem();
return false;
return {};
}
unsigned long len = out.size();
auto ret = ecc_sign_hash_ex(reinterpret_cast<const unsigned char *>(hash.ptr), hash.length,
out.data(), &len,
&yarrow_prng,
::Crypto::gPrngYarrow,
LTC_ECCSIG_ETH27,
nullptr,
&_key);
unsigned long len = 1024;
auto ret = ::ecc_sign_hash_ex(AuReinterpretCast<const unsigned char *>(hash.ptr), hash.length,
out.writePtr, &len,
&yarrow_prng,
::Crypto::gPrngYarrow,
LTC_ECCSIG_ETH27,
nullptr,
&this->_key);
if (ret != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
return false;
}
if (!AuTryResize(out, len))
{
SysPushErrorMem();
return false;
}
out.writePtr += len;
return true;
}
@ -118,44 +115,42 @@ namespace Aurora::Crypto::ECC
bool PrivateECCImpl::ECDH(const AuSPtr<IECCPublic> &partnerPublic,
AuByteBuffer &sharedKey)
{
AuByteBuffer sharedSecret;
if (!AuTryResize(sharedSecret, 128))
auto writeView = sharedKey.GetOrAllocateLinearWriteable(128);
if (!writeView)
{
SysPushErrorMem();
return false;
return {};
}
if (partnerPublic->GetType() == this->GetType())
{
SysPushErrorCrypto("Can not EDCH with incompatible curve curve type (noting, ed25519 requires translation to x25519)");
return false;
}
unsigned long actualSize = sharedSecret.size();
auto ret = ecc_shared_secret(&_key, &(AuReinterpretCast<PublicECCImpl>(partnerPublic)->GetKey()), sharedSecret.data(), &actualSize);
unsigned long actualSize = 128;
auto ret = ::ecc_shared_secret(&this->_key,
&(AuReinterpretCast<PublicECCImpl>(partnerPublic)->GetKey()),
sharedKey.writePtr,
&actualSize);
if (ret != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
return false;
}
if (!AuTryResize(sharedSecret, actualSize))
{
SysPushErrorMem();
return false;
}
sharedKey.writePtr += actualSize;
return true;
}
bool PrivateECCImpl::AsPublicECC(AuByteBuffer &out)
{
return ExportECCKey(_key, true, out);
return ExportECCKey(this->_key, true, out);
}
bool PrivateECCImpl::AsPrivateECC(AuByteBuffer &out)
{
return ExportECCKey(_key, false, out);
return ExportECCKey(this->_key, false, out);
}
}

View File

@ -88,12 +88,12 @@ namespace Aurora::Crypto::ECC
}
unsigned long hashSize = hashVec.size();
auto ret = hash_memory(hash,
reinterpret_cast<const unsigned char *>(plaintext.ptr), plaintext.length,
reinterpret_cast<unsigned char *>(hashVec.data()), &hashSize);
if (ret != CRYPT_OK)
auto iRet = ::hash_memory(hash,
AuReinterpretCast<const unsigned char *>(plaintext.ptr), plaintext.length,
AuReinterpretCast<unsigned char *>(hashVec.data()), &hashSize);
if (iRet != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
SysPushErrorCrypt("{}", iRet);
return false;
}

View File

@ -20,7 +20,7 @@ namespace Aurora::Crypto::RSA
PrivateRSA::~PrivateRSA()
{
rsa_free(&key_);
::rsa_free(&this->key_);
}
bool PrivateRSA::Sign(const Memory::MemoryViewRead & payload,
@ -51,10 +51,11 @@ namespace Aurora::Crypto::RSA
return false;
}
if (!AuTryResize(out, 1024))
auto writeView = out.GetOrAllocateLinearWriteable(1024);
if (!writeView)
{
SysPushErrorMem();
return false;
return {};
}
Memory::ByteBuffer hashVec;
@ -66,31 +67,31 @@ namespace Aurora::Crypto::RSA
}
unsigned long hashSize = hashVec.size();
auto ret = hash_memory(hash,
reinterpret_cast<const unsigned char *>(payload.ptr), payload.length,
reinterpret_cast<unsigned char *>(hashVec.data()), &hashSize);
if (ret != CRYPT_OK)
auto iRet = ::hash_memory(hash,
AuReinterpretCast<const unsigned char *>(payload.ptr), payload.length,
AuReinterpretCast<unsigned char *>(hashVec.data()), &hashSize);
if (iRet != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
SysPushErrorCrypt("{}", iRet);
return false;
}
unsigned long len = out.size();
ret = rsa_sign_hash_ex(reinterpret_cast<const unsigned char *>(hashVec.data()), hashSize,
out.data(), &len,
padding,
&yarrow_prng,
::Crypto::gPrngYarrow,
hash,
salt,
&key_);
if (ret != CRYPT_OK)
unsigned long len = 1024;
iRet = ::rsa_sign_hash_ex(AuReinterpretCast<const unsigned char *>(hashVec.data()), hashSize,
out.writePtr, &len,
padding,
&yarrow_prng,
::Crypto::gPrngYarrow,
hash,
salt,
&this->key_);
if (iRet != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
SysPushErrorCrypt("{}", iRet);
return false;
}
out.resize(len);
out.writePtr += len;
return true;
}
@ -118,40 +119,42 @@ namespace Aurora::Crypto::RSA
return false;
}
if (!AuTryResize(out, payload.length))
auto writeView = out.GetOrAllocateLinearWriteable(payload.length);
if (!writeView)
{
SysPushErrorMem();
return false;
return {};
}
unsigned long len = out.size();
unsigned long len = payload.length;
int stat = 0;
auto ret = rsa_decrypt_key_ex(reinterpret_cast<const unsigned char *>(payload.ptr), payload.length,
out.data(), &len,
NULL, 0,
0, // hash? excuse me?
padding,
&stat,
&key_);
auto ret = ::rsa_decrypt_key_ex(AuReinterpretCast<const unsigned char *>(payload.ptr),
payload.length,
out.writePtr, &len,
NULL, 0,
0, // hash? excuse me?
padding,
&stat,
&this->key_);
if (ret != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
return false;
}
out.resize(len);
out.writePtr += len;
return stat == 1;
}
AuSPtr<IRSAPublic> PrivateRSA::ToPublic()
{
return AuMakeShared<PublicRSA>(key_, false);
return AuMakeShared<PublicRSA>(this->key_, false);
}
bool PrivateRSA::ToKey(const RSAMeta &meta, AuByteBuffer &out)
{
return ExportRSAKey(key_, meta.side, meta.type, out);
return ExportRSAKey(this->key_, meta.side, meta.type, out);
}
AUKN_SYM IRSAPrivate *OpenRSAPrivateNew(const RSAKey &key)
@ -169,19 +172,19 @@ namespace Aurora::Crypto::RSA
return nullptr;
}
auto ret = _new PrivateRSA(in);
if (!ret)
auto pPrivate = _new PrivateRSA(in);
if (!pPrivate)
{
rsa_free(&in);
::rsa_free(&in);
return nullptr;
}
return ret;
return pPrivate;
}
AUKN_SYM void OpenRSAPrivateRelease(IRSAPrivate *re)
AUKN_SYM void OpenRSAPrivateRelease(IRSAPrivate *pPrivate)
{
AuSafeDelete<PrivateRSA *>(re);
AuSafeDelete<PrivateRSA *>(pPrivate);
}
AUKN_SYM IRSAPrivate *NewRSAKeyNew(AuUInt16 keySize)
@ -190,17 +193,17 @@ namespace Aurora::Crypto::RSA
rsa_key key {};
const int prng_idx = register_prng(&sprng_desc);
auto error = rsa_make_key(NULL, prng_idx, keySize / 8, 65537, &key);
if (error != CRYPT_OK)
auto iRet = ::rsa_make_key(NULL, prng_idx, keySize / 8, 65537, &key);
if (iRet != CRYPT_OK)
{
SysPushErrorCrypt("{}", error);
SysPushErrorCrypt("{}", iRet);
return nullptr;
}
auto ret = _new PrivateRSA(key);
if (!ret)
{
rsa_free(&key);
::rsa_free(&key);
return nullptr;
}

View File

@ -19,9 +19,9 @@ namespace Aurora::Crypto::RSA
PublicRSA::~PublicRSA()
{
if (owned_)
if (this->owned_)
{
rsa_free(&key_);
::rsa_free(&this->key_);
}
}
@ -65,23 +65,23 @@ namespace Aurora::Crypto::RSA
}
unsigned long hashSize = hashVec.size();
auto ret = hash_memory(hash,
reinterpret_cast<const unsigned char *>(payload.ptr), payload.length,
reinterpret_cast<unsigned char *>(hashVec.data()), &hashSize);
if (ret != CRYPT_OK)
auto iRet = ::hash_memory(hash,
AuReinterpretCast<const unsigned char *>(payload.ptr), payload.length,
AuReinterpretCast<unsigned char *>(hashVec.data()), &hashSize);
if (iRet != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
SysPushErrorCrypt("{}", iRet);
return false;
}
int ok = 0;
ret = rsa_verify_hash_ex(reinterpret_cast<const unsigned char *>(signature.ptr), signature.length,
reinterpret_cast<const unsigned char *>(hashVec.data()), hashSize,
padding, hash, 0, &ok, &key_);
if (ret != CRYPT_OK)
iRet = ::rsa_verify_hash_ex(AuReinterpretCast<const unsigned char *>(signature.ptr), signature.length,
AuReinterpretCast<const unsigned char *>(hashVec.data()), hashSize,
padding, hash, 0, &ok, &this->key_);
if (iRet != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
SysPushErrorCrypt("{}", iRet);
return false;
}
@ -114,35 +114,36 @@ namespace Aurora::Crypto::RSA
return false;
}
if (!AuTryResize(out, plainText.length + 1024))
auto writeView = out.GetOrAllocateLinearWriteable(plainText.length + 1024);
if (!writeView)
{
SysPushErrorMem();
return false;
return {};
}
unsigned long len = out.size();
auto ret = rsa_encrypt_key_ex(reinterpret_cast<const unsigned char *>(plainText.ptr),
plainText.length,
out.data(), &len,
NULL, 0,
&yarrow_prng, prng_idx,
0,
padding,
&key_);
if (ret != CRYPT_OK)
auto iREt = ::rsa_encrypt_key_ex(AuReinterpretCast<const unsigned char *>(plainText.ptr),
plainText.length,
out.writePtr, &len,
NULL, 0,
&yarrow_prng, prng_idx,
0,
padding,
&this->key_);
if (iREt != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
SysPushErrorCrypt("{}", iREt);
return false;
}
out.resize(len);
out.writePtr += len;
return true;
}
bool PublicRSA::ToKey(ERSAKeyType type, AuByteBuffer &out)
{
return ExportRSAKey(key_, EKeyType::eKeyPublic, type, out);
return ExportRSAKey(this->key_, EKeyType::eKeyPublic, type, out);
}
AUKN_SYM IRSAPublic *OpenRSAPublicNew(const RSAKey &key)
@ -154,18 +155,18 @@ namespace Aurora::Crypto::RSA
return nullptr;
}
auto ret = _new PublicRSA(in, true);
if (!ret)
auto pPublic = _new PublicRSA(in, true);
if (!pPublic)
{
rsa_free(&in);
::rsa_free(&in);
return nullptr;
}
return ret;
return pPublic;
}
AUKN_SYM void OpenRSAPublicRelease(IRSAPublic *re)
AUKN_SYM void OpenRSAPublicRelease(IRSAPublic *pPublic)
{
AuSafeDelete<PublicRSA *>(re);
AuSafeDelete<PublicRSA *>(pPublic);
}
}

View File

@ -235,6 +235,7 @@ namespace Aurora::IO::FS
std::wstring win32Path;
LARGE_INTEGER length;
bool status;
AuMemoryViewWrite writeView;
size_t offset;
status = false;
@ -259,10 +260,10 @@ namespace Aurora::IO::FS
goto out;
}
if (!AuTryResize(buffer, length.QuadPart))
writeView = buffer.GetOrAllocateLinearWriteable(length.QuadPart);
if (!writeView)
{
SysPushErrorMem();
goto out;
return {};
}
while (length.QuadPart)
@ -271,13 +272,14 @@ namespace Aurora::IO::FS
int blockSize = AuMin(static_cast<AuUInt64>(kFileCopyBlock), static_cast<AuUInt64>(length.QuadPart));
if (!::ReadFile(fileHandle, &buffer[offset], blockSize, &read, NULL))
if (!::ReadFile(fileHandle, buffer.writePtr, blockSize, &read, NULL))
{
AuLogWarn("ReadFile IO Error: 0x{:x} {}", GetLastError(), path);
SysPushErrorIO();
goto out;
}
buffer.writePtr += read;
offset += read;
length.QuadPart -= read;
}

View File

@ -212,6 +212,7 @@ namespace Aurora::IO::FS
AUKN_SYM bool ReadFile(const AuString &path, AuByteBuffer &buffer)
{
AuMemoryViewWrite writeView;
AuUInt read;
auto file = OpenReadUnique(path, EFileAdvisoryLockLevel::eNoSafety);
@ -236,13 +237,13 @@ namespace Aurora::IO::FS
}
}
if (!AuTryResize(buffer, len))
writeView = buffer.GetOrAllocateLinearWriteable(length);
if (!writeView)
{
SysPushErrorMem();
return false;
return {};
}
if (!file->Read(Memory::MemoryViewStreamWrite {buffer.begin(), buffer.end(), read}))
if (!file->Read(Memory::MemoryViewStreamWrite { writeView, read}))
{
SysPushErrorIO();
return false;
@ -251,7 +252,8 @@ namespace Aurora::IO::FS
// NOTE: File devices love to lie
// Do not entertain an arbitrarily large page length provided by non-regular fds
return AuTryResize(buffer, read);
buffer.writePtr += read;
return true;
}
static bool UnixExists(const AuString &path, bool dir)