[*] potential bug in x25519 sign: was using the linear base ptr instead of write after alloc or validate

[*] old copy/pasted ecdh function clearly wasnt tested in its now form :(
[*] clean up
This commit is contained in:
Reece Wilson 2022-09-25 09:25:33 +01:00
parent 184fecb8ab
commit 0ded2ec77c
7 changed files with 30 additions and 28 deletions

View File

@ -12,7 +12,7 @@
namespace Aurora::Crypto::ECC
{
PrivateCurve25519Impl::PrivateCurve25519Impl(bool isX25519, curve25519_key &&key) : isX25519_(isX25519), key_(key)
PrivateCurve25519Impl::PrivateCurve25519Impl(bool isX25519, curve25519_key &&key) : bIsX25519_(isX25519), key_(key)
{
}
@ -28,7 +28,7 @@ namespace Aurora::Crypto::ECC
{
const int salt = 0;
if (this->isX25519_)
if (this->bIsX25519_)
{
return false;
}
@ -69,7 +69,7 @@ namespace Aurora::Crypto::ECC
bool PrivateCurve25519Impl::Sign(const Memory::MemoryViewRead &hash,
Memory::ByteBuffer &out)
{
if (this->isX25519_)
if (this->bIsX25519_)
{
return false;
}
@ -80,8 +80,7 @@ namespace Aurora::Crypto::ECC
return {};
}
auto writeView = out.GetOrAllocateLinearWriteable(1024);
if (!writeView)
if (!out.GetOrAllocateLinearWriteable(1024))
{
SysPushErrorMem();
return {};
@ -89,9 +88,10 @@ namespace Aurora::Crypto::ECC
unsigned long len = 1024;
auto iRet = ::ed25519_sign(AuReinterpretCast<const unsigned char *>(hash.ptr), hash.length,
out.data(), &len,
&key_);
auto iRet = ::ed25519_sign(AuReinterpretCast<const unsigned char *>(hash.ptr),
hash.length,
out.writePtr, &len,
&this->key_);
if (iRet != CRYPT_OK)
{
SysPushErrorCrypt("{}", iRet);
@ -106,7 +106,7 @@ namespace Aurora::Crypto::ECC
bool PrivateCurve25519Impl::ECDH(const AuSPtr<IECCPublic> &partnerPublic,
Memory::ByteBuffer &sharedKey)
{
if (!this->isX25519_)
if (!this->bIsX25519_)
{
return false;
}
@ -118,9 +118,9 @@ namespace Aurora::Crypto::ECC
return {};
}
if (partnerPublic->GetType() == this->GetType())
if (partnerPublic->GetType() != this->GetType())
{
SysPushErrorCrypto("Can not EDCH with incompatible curve curve type (noting, ed25519 requires translation to x25519)");
SysPushErrorCrypto("Can not EDCH with incompatible pairs curve (noting ed25519 requires translation to x25519)");
return false;
}
@ -153,7 +153,7 @@ namespace Aurora::Crypto::ECC
actualSize = 4096;
if (this->isX25519_)
if (this->bIsX25519_)
{
ret = x25519_export(out.writePtr, &actualSize, PK_PUBLIC, &this->key_);
}
@ -187,7 +187,7 @@ namespace Aurora::Crypto::ECC
actualSize = 4096;
if (this->isX25519_)
if (this->bIsX25519_)
{
ret = x25519_export(out.writePtr, &actualSize, PK_PRIVATE, &this->key_);
}
@ -209,6 +209,6 @@ namespace Aurora::Crypto::ECC
EECCCurve PrivateCurve25519Impl::GetType()
{
return this->isX25519_ ? EECCCurve::eCurveX25519 : EECCCurve::eCurveEd25519;
return this->bIsX25519_ ? EECCCurve::eCurveX25519 : EECCCurve::eCurveEd25519;
}
}

View File

@ -9,9 +9,8 @@
namespace Aurora::Crypto::ECC
{
class PrivateCurve25519Impl : public IECCPrivate
struct PrivateCurve25519Impl : IECCPrivate
{
public:
PrivateCurve25519Impl(bool isX25519, curve25519_key &&key);
~PrivateCurve25519Impl();
@ -30,8 +29,9 @@ namespace Aurora::Crypto::ECC
bool AsPrivateECC(Memory::ByteBuffer &out) override;
EECCCurve GetType() override;
private:
curve25519_key key_;
bool isX25519_;
bool bIsX25519_;
};
}

View File

@ -9,9 +9,8 @@
namespace Aurora::Crypto::ECC
{
class PublicCurve25519Impl : public IECCPublic
struct PublicCurve25519Impl : IECCPublic
{
public:
PublicCurve25519Impl(bool isX25519, curve25519_key &&key);
~PublicCurve25519Impl();

View File

@ -1,8 +1,11 @@
/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: PrivateECCImpl.cpp
File: ECCGeneric.cpp
Date: 2021-9-17
File: KCryptoECC.cpp
Date: 2021-1-15
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
@ -86,8 +89,7 @@ namespace Aurora::Crypto::ECC
return {};
}
auto writeView = out.GetOrAllocateLinearWriteable(1024);
if (!writeView)
if (!out.GetOrAllocateLinearWriteable(1024))
{
SysPushErrorMem();
return {};
@ -122,9 +124,9 @@ namespace Aurora::Crypto::ECC
return {};
}
if (partnerPublic->GetType() == this->GetType())
if (partnerPublic->GetType() != this->GetType())
{
SysPushErrorCrypto("Can not EDCH with incompatible curve curve type (noting, ed25519 requires translation to x25519)");
SysPushErrorCrypto("Can not EDCH with incompatible curve type (noting ed25519 requires translation to x25519)");
return false;
}

View File

@ -2,9 +2,8 @@
namespace Aurora::Crypto::ECC
{
class PrivateECCImpl : public IECCPrivate
struct PrivateECCImpl : IECCPrivate
{
public:
PrivateECCImpl(EECCCurve type, ecc_key &key);
~PrivateECCImpl();

View File

@ -1,8 +1,11 @@
/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: PublicECCImpl.cpp
File: ECCGeneric.cpp
Date: 2021-9-17
File: KCryptoECC.cpp
Date: 2021-1-15
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>

View File

@ -2,9 +2,8 @@
namespace Aurora::Crypto::ECC
{
class PublicECCImpl : public IECCPublic
struct PublicECCImpl : IECCPublic
{
public:
PublicECCImpl(EECCCurve type, ecc_key &key);
~PublicECCImpl();