[*] ByteBuffer::begin() and ::end() now guarantee a linear view of available bytes in either ring or linear bytebuffers. This means ::begin() is no longer guranteed to return the base. In almost all real world use cases, this does not matter. In fact, this is what we want to preserve legacy code with streamable buffers.
[*] Update KCryptoAES: use streamable buffers. deprecate legacy vec<8>::size() based logic - AuByteBuffer (replacing vec8)::begin, ::end still works. [*] IO pipe buffers should no longer be marked as circular
This commit is contained in:
parent
1de0bdb4e7
commit
184fecb8ab
@ -62,14 +62,33 @@ namespace Aurora::Memory
|
||||
|
||||
AuUInt8 *ByteBuffer::begin() const
|
||||
{
|
||||
SysAssert(!flagCircular, "::begin is only available for linear buffers");
|
||||
return base;
|
||||
return this->readPtr;
|
||||
}
|
||||
|
||||
AuUInt8 *ByteBuffer::end() const
|
||||
{
|
||||
SysAssert(!flagCircular, "::end is only available for linear buffers");
|
||||
return base + length;
|
||||
AuUInt8 *pBase {};
|
||||
AuUInt uCount {};
|
||||
|
||||
auto readPtr = this->readPtr;
|
||||
|
||||
if (this->flagCircular && this->base + this->length == readPtr)
|
||||
{
|
||||
readPtr = this->base;
|
||||
}
|
||||
|
||||
if (this->writePtr >= readPtr)
|
||||
{
|
||||
uCount = this->writePtr - readPtr;
|
||||
pBase = readPtr;
|
||||
}
|
||||
else if (this->flagCircular)
|
||||
{
|
||||
uCount = (this->base + this->length) - readPtr;
|
||||
pBase = readPtr;
|
||||
}
|
||||
|
||||
return uCount + pBase;
|
||||
}
|
||||
|
||||
AuUInt32 ByteBuffer::GetAllocationPower() const
|
||||
|
@ -49,8 +49,8 @@ namespace Aurora::Crypto::AES
|
||||
auto padding = 128 - overhang;
|
||||
auto primaryLength = plainTextLength - overhang;
|
||||
|
||||
int ret;
|
||||
auto tptr = reinterpret_cast<const unsigned char *>(plainText);
|
||||
int iRet;
|
||||
auto tptr = AuReinterpretCast<const unsigned char *>(plainText);
|
||||
|
||||
unsigned char pad[128] =
|
||||
{
|
||||
@ -72,68 +72,74 @@ namespace Aurora::Crypto::AES
|
||||
auto lastbyte = tptr[primaryLength - 1];
|
||||
if (lastbyte <= 128)
|
||||
{
|
||||
if (!AuTryResize(out, primaryLength + 128))
|
||||
if (!out.GetOrAllocateLinearWriteable(primaryLength + 128))
|
||||
{
|
||||
SysPushErrorMem();
|
||||
return false;
|
||||
}
|
||||
|
||||
// encrypt plain text
|
||||
ret = cbc_encrypt(tptr, out.data(), primaryLength, &cbc);
|
||||
if (ret != CRYPT_OK)
|
||||
iRet = cbc_encrypt(tptr, out.writePtr, primaryLength, &cbc);
|
||||
if (iRet != CRYPT_OK)
|
||||
{
|
||||
SysPushErrorCrypto("{}", ret);
|
||||
SysPushErrorCrypto("{}", iRet);
|
||||
return false;
|
||||
}
|
||||
|
||||
// encrypt another 128 bytes of trash :( 50/50 chance
|
||||
ret = cbc_encrypt(pad, out.data() + primaryLength, 128, &cbc);
|
||||
if (ret != CRYPT_OK)
|
||||
iRet = cbc_encrypt(pad, out.writePtr + primaryLength, 128, &cbc);
|
||||
if (iRet != CRYPT_OK)
|
||||
{
|
||||
SysPushErrorCrypto("{}", ret);
|
||||
SysPushErrorCrypto("{}", iRet);
|
||||
return false;
|
||||
}
|
||||
|
||||
out.writePtr += primaryLength + 128;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!AuTryResize(out, primaryLength))
|
||||
if (!out.GetOrAllocateLinearWriteable(primaryLength))
|
||||
{
|
||||
SysPushErrorMem();
|
||||
return false;
|
||||
}
|
||||
|
||||
ret = cbc_encrypt(tptr, out.data(), primaryLength, &cbc);
|
||||
if (ret != CRYPT_OK)
|
||||
iRet = cbc_encrypt(tptr, out.writePtr, primaryLength, &cbc);
|
||||
if (iRet != CRYPT_OK)
|
||||
{
|
||||
SysPushErrorCrypto("{}", ret);
|
||||
SysPushErrorCrypto("{}", iRet);
|
||||
return false;
|
||||
}
|
||||
|
||||
out.writePtr += primaryLength;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!AuTryResize(out, primaryLength + 128))
|
||||
if (!out.GetOrAllocateLinearWriteable(primaryLength + 128))
|
||||
{
|
||||
SysPushErrorMem();
|
||||
return false;
|
||||
}
|
||||
|
||||
ret = cbc_encrypt(tptr, out.data(), primaryLength, &cbc);
|
||||
if (ret != CRYPT_OK)
|
||||
iRet = cbc_encrypt(tptr, out.writePtr, primaryLength, &cbc);
|
||||
if (iRet != CRYPT_OK)
|
||||
{
|
||||
SysPushErrorCrypto("{}", ret);
|
||||
SysPushErrorCrypto("{}", iRet);
|
||||
return false;
|
||||
}
|
||||
|
||||
AuMemcpy(pad, tptr + primaryLength, overhang);
|
||||
pad[127] = padding;
|
||||
|
||||
ret = cbc_encrypt(pad, out.data() + primaryLength, 128, &cbc);
|
||||
if (ret != CRYPT_OK)
|
||||
iRet = cbc_encrypt(pad, out.writePtr + primaryLength, 128, &cbc);
|
||||
if (iRet != CRYPT_OK)
|
||||
{
|
||||
SysPushErrorCrypto("{}", ret);
|
||||
SysPushErrorCrypto("{}", iRet);
|
||||
return false;
|
||||
}
|
||||
|
||||
out.writePtr += primaryLength + 128;
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -188,23 +194,29 @@ namespace Aurora::Crypto::AES
|
||||
return false;
|
||||
}
|
||||
|
||||
auto ret = cbc_start(::Crypto::gAesCipher,
|
||||
reinterpret_cast<const unsigned char *>(inIv.ptr),
|
||||
reinterpret_cast<const unsigned char *>(inKey.ptr),
|
||||
inKey.length,
|
||||
0,
|
||||
&cbc);
|
||||
if (ret != CRYPT_OK)
|
||||
auto iRet = cbc_start(::Crypto::gAesCipher,
|
||||
AuReinterpretCast<const unsigned char *>(inIv.ptr),
|
||||
AuReinterpretCast<const unsigned char *>(inKey.ptr),
|
||||
inKey.length,
|
||||
0,
|
||||
&cbc);
|
||||
if (iRet != CRYPT_OK)
|
||||
{
|
||||
SysPushErrorCrypt("{}", ret);
|
||||
SysPushErrorCrypt("{}", iRet);
|
||||
return false;
|
||||
}
|
||||
|
||||
auto tptr = reinterpret_cast<const unsigned char *>(plainText.ptr);
|
||||
|
||||
if (auCoolCodePadding)
|
||||
{
|
||||
if (!EncryptCoolCodePadding(tptr, plainText.length, inIv.ptr, outIv.ptr, inIv.length, inKey.ptr, inKey.length, out, cbc))
|
||||
if (!EncryptCoolCodePadding(AuReinterpretCast<const unsigned char *>(plainText.ptr),
|
||||
plainText.length,
|
||||
inIv.ptr,
|
||||
outIv.ptr,
|
||||
inIv.length,
|
||||
inKey.ptr,
|
||||
inKey.length,
|
||||
out,
|
||||
cbc))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@ -217,22 +229,36 @@ namespace Aurora::Crypto::AES
|
||||
return false;
|
||||
}
|
||||
|
||||
ret = cbc_encrypt(tptr, out.data(), plainText.length, &cbc);
|
||||
if (ret != CRYPT_OK)
|
||||
if (!out.GetOrAllocateLinearWriteable(plainText.length))
|
||||
{
|
||||
SysPushErrorCrypt("{}", ret);
|
||||
SysPushErrorMem();
|
||||
return false;
|
||||
}
|
||||
|
||||
iRet = cbc_encrypt(AuReinterpretCast<const unsigned char *>(plainText.ptr),
|
||||
out.writePtr,
|
||||
plainText.length,
|
||||
&cbc);
|
||||
if (iRet != CRYPT_OK)
|
||||
{
|
||||
SysPushErrorCrypt("{}", iRet);
|
||||
return false;
|
||||
}
|
||||
|
||||
out.writePtr += plainText.length;
|
||||
}
|
||||
|
||||
if (outIv.ptr)
|
||||
{
|
||||
unsigned long ivLength = 16;
|
||||
ret = cbc_getiv(reinterpret_cast<unsigned char *>(outIv.ptr), &ivLength, &cbc);
|
||||
|
||||
if (ret != CRYPT_OK)
|
||||
iRet = cbc_getiv(AuReinterpretCast<unsigned char *>(outIv.ptr),
|
||||
&ivLength,
|
||||
&cbc);
|
||||
|
||||
if (iRet != CRYPT_OK)
|
||||
{
|
||||
SysPushErrorCrypt("{}", ret);
|
||||
SysPushErrorCrypt("{}", iRet);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -240,7 +266,6 @@ namespace Aurora::Crypto::AES
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
AUKN_SYM bool Decrypt(const AuMemoryViewRead &cipherText,
|
||||
const AuMemoryViewRead &inIv,
|
||||
const AuMemoryViewWrite &outIv,
|
||||
@ -291,30 +316,32 @@ namespace Aurora::Crypto::AES
|
||||
SysPushErrorCrypt("{}", inKey.length);
|
||||
return false;
|
||||
}
|
||||
auto ret = cbc_start(::Crypto::gAesCipher,
|
||||
reinterpret_cast<const unsigned char *>(inIv.ptr),
|
||||
reinterpret_cast<const unsigned char *>(inKey.ptr),
|
||||
inKey.length,
|
||||
0,
|
||||
&cbc);
|
||||
if (ret != CRYPT_OK)
|
||||
|
||||
auto iRet = cbc_start(::Crypto::gAesCipher,
|
||||
AuReinterpretCast<const unsigned char *>(inIv.ptr),
|
||||
AuReinterpretCast<const unsigned char *>(inKey.ptr),
|
||||
inKey.length,
|
||||
0,
|
||||
&cbc);
|
||||
if (iRet != CRYPT_OK)
|
||||
{
|
||||
SysPushErrorCrypt("{}", ret);
|
||||
SysPushErrorCrypt("{}", iRet);
|
||||
return false;
|
||||
}
|
||||
|
||||
auto tptr = reinterpret_cast<const unsigned char *>(cipherText.ptr);
|
||||
|
||||
if (!AuTryResize(plainText, cipherText.length))
|
||||
if (!plainText.GetOrAllocateLinearWriteable(cipherText.length))
|
||||
{
|
||||
SysPushErrorMem();
|
||||
return false;
|
||||
}
|
||||
|
||||
ret = cbc_decrypt(tptr, plainText.data(), cipherText.length, &cbc);
|
||||
if (ret != CRYPT_OK)
|
||||
iRet = cbc_decrypt(AuReinterpretCast<const unsigned char *>(cipherText.ptr),
|
||||
plainText.writePtr,
|
||||
cipherText.length,
|
||||
&cbc);
|
||||
if (iRet != CRYPT_OK)
|
||||
{
|
||||
SysPushErrorCrypt("{}", ret);
|
||||
SysPushErrorCrypt("{}", iRet);
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -323,22 +350,30 @@ namespace Aurora::Crypto::AES
|
||||
auto magic = plainText[cipherText.length - 1];
|
||||
if (magic <= 128)
|
||||
{
|
||||
plainText.resize(cipherText.length - magic);
|
||||
plainText.writePtr += cipherText.length - magic;
|
||||
}
|
||||
else
|
||||
{
|
||||
plainText.writePtr += cipherText.length;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
plainText.writePtr += cipherText.length;
|
||||
}
|
||||
|
||||
if (outIv.ptr)
|
||||
{
|
||||
unsigned long ivLength = 16;
|
||||
ret = cbc_getiv(reinterpret_cast<unsigned char *>(outIv.ptr), &ivLength, &cbc);
|
||||
if (ret != CRYPT_OK)
|
||||
{
|
||||
|
||||
SysPushErrorCrypt("{}", ret);
|
||||
iRet = cbc_getiv(reinterpret_cast<unsigned char *>(outIv.ptr), &ivLength, &cbc);
|
||||
if (iRet != CRYPT_OK)
|
||||
{
|
||||
SysPushErrorCrypt("{}", iRet);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -171,8 +171,6 @@ namespace Aurora::IO
|
||||
TerminateOnThread(true);
|
||||
return;
|
||||
}
|
||||
|
||||
this->buffer_.flagCircular = true; // !!!
|
||||
}
|
||||
|
||||
void IOPipeWork::PrepareAsync()
|
||||
|
Loading…
Reference in New Issue
Block a user