2021-09-21 01:54:47 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
2022-09-25 08:25:33 +00:00
|
|
|
|
|
|
|
File: PrivateECCImpl.cpp
|
2021-09-21 01:54:47 +00:00
|
|
|
File: ECCGeneric.cpp
|
|
|
|
Date: 2021-9-17
|
2022-09-25 08:25:33 +00:00
|
|
|
File: KCryptoECC.cpp
|
|
|
|
Date: 2021-1-15
|
2021-09-21 01:54:47 +00:00
|
|
|
Author: Reece
|
|
|
|
***/
|
2021-09-30 14:57:41 +00:00
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2021-09-21 01:54:47 +00:00
|
|
|
#include "ECC.hpp"
|
|
|
|
#include "ECCGeneric.hpp"
|
|
|
|
#include "ECCCurves.hpp"
|
|
|
|
#include "PrivateECCImpl.hpp"
|
|
|
|
#include "PublicECCImpl.hpp"
|
|
|
|
|
|
|
|
namespace Aurora::Crypto::ECC
|
|
|
|
{
|
2022-08-28 19:02:06 +00:00
|
|
|
PrivateECCImpl::PrivateECCImpl(EECCCurve type, ecc_key &key) : _key(key), _type(type)
|
2021-09-21 01:54:47 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PrivateECCImpl::~PrivateECCImpl()
|
|
|
|
{
|
2022-09-19 05:42:39 +00:00
|
|
|
ecc_free(&this->_key);
|
2021-09-21 01:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EECCCurve PrivateECCImpl::GetType()
|
|
|
|
{
|
2022-09-19 05:42:39 +00:00
|
|
|
return this->_type;
|
2021-09-21 01:54:47 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 16:37:22 +00:00
|
|
|
bool PrivateECCImpl::Sign(const AuMemoryViewRead &plainText,
|
2022-09-25 09:56:03 +00:00
|
|
|
AuHashing::EHashType method,
|
2022-01-20 16:37:22 +00:00
|
|
|
AuByteBuffer &out)
|
2021-09-21 01:54:47 +00:00
|
|
|
{
|
|
|
|
const int salt = 0;
|
|
|
|
|
|
|
|
if (!plainText.HasMemory())
|
|
|
|
{
|
|
|
|
SysPushErrorParam();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-09-25 09:56:03 +00:00
|
|
|
int hash = ::Crypto::HashMethodToId(method);
|
2021-09-21 01:54:47 +00:00
|
|
|
if (hash == 0xFF)
|
|
|
|
{
|
2024-02-16 13:07:28 +00:00
|
|
|
SysPushErrorCrypt("invalid hash {}", AuUInt(method));
|
2021-09-21 01:54:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AuTryResize(out, 1024))
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-20 16:37:22 +00:00
|
|
|
AuByteBuffer hashVec;
|
2021-09-21 01:54:47 +00:00
|
|
|
|
|
|
|
if (!AuTryResize(hashVec, 128))
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long hashSize = hashVec.size();
|
2022-09-19 05:42:39 +00:00
|
|
|
auto ret = ::hash_memory(hash,
|
|
|
|
AuReinterpretCast<const unsigned char *>(plainText.ptr), plainText.length,
|
|
|
|
AuReinterpretCast<unsigned char *>(hashVec.data()), &hashSize);
|
2021-09-21 01:54:47 +00:00
|
|
|
if (ret != CRYPT_OK)
|
|
|
|
{
|
|
|
|
SysPushErrorCrypt("{}", ret);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Sign(hashVec, out);
|
|
|
|
}
|
|
|
|
|
2022-01-20 16:37:22 +00:00
|
|
|
bool PrivateECCImpl::Sign(const AuMemoryViewRead &hash,
|
|
|
|
AuByteBuffer &out)
|
2021-09-21 01:54:47 +00:00
|
|
|
{
|
2022-09-25 10:19:43 +00:00
|
|
|
int iRet;
|
2021-09-21 01:54:47 +00:00
|
|
|
prng_state yarrow_prng;
|
|
|
|
|
|
|
|
if (!hash.HasMemory())
|
|
|
|
{
|
|
|
|
SysPushErrorParam();
|
|
|
|
return {};
|
|
|
|
}
|
2022-09-19 05:42:39 +00:00
|
|
|
|
2022-09-25 08:25:33 +00:00
|
|
|
if (!out.GetOrAllocateLinearWriteable(1024))
|
2021-09-21 01:54:47 +00:00
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
2022-09-19 05:42:39 +00:00
|
|
|
return {};
|
2021-09-21 01:54:47 +00:00
|
|
|
}
|
|
|
|
|
2022-09-25 10:19:43 +00:00
|
|
|
iRet = yarrow_start(&yarrow_prng);
|
|
|
|
if (iRet != CRYPT_OK)
|
|
|
|
{
|
|
|
|
SysPushErrorCrypt("{}", iRet);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-19 05:42:39 +00:00
|
|
|
unsigned long len = 1024;
|
2022-09-25 10:19:43 +00:00
|
|
|
iRet = ::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 (iRet != CRYPT_OK)
|
2021-09-21 01:54:47 +00:00
|
|
|
{
|
2022-09-25 10:19:43 +00:00
|
|
|
SysPushErrorCrypt("{}", iRet);
|
2021-09-21 01:54:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-09-25 10:19:43 +00:00
|
|
|
|
2022-09-19 05:42:39 +00:00
|
|
|
out.writePtr += len;
|
2021-09-21 01:54:47 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PrivateECCImpl::ECDH(const AuSPtr<IECCPublic> &partnerPublic,
|
2022-01-20 16:37:22 +00:00
|
|
|
AuByteBuffer &sharedKey)
|
2021-09-21 01:54:47 +00:00
|
|
|
{
|
2022-09-19 05:42:39 +00:00
|
|
|
auto writeView = sharedKey.GetOrAllocateLinearWriteable(128);
|
|
|
|
if (!writeView)
|
2021-09-21 01:54:47 +00:00
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
2022-09-19 05:42:39 +00:00
|
|
|
return {};
|
2021-09-21 01:54:47 +00:00
|
|
|
}
|
2022-09-19 05:42:39 +00:00
|
|
|
|
2022-09-25 08:25:33 +00:00
|
|
|
if (partnerPublic->GetType() != this->GetType())
|
2021-09-21 01:54:47 +00:00
|
|
|
{
|
2022-09-25 08:25:33 +00:00
|
|
|
SysPushErrorCrypto("Can not EDCH with incompatible curve type (noting ed25519 requires translation to x25519)");
|
2021-09-21 01:54:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-19 05:42:39 +00:00
|
|
|
unsigned long actualSize = 128;
|
|
|
|
auto ret = ::ecc_shared_secret(&this->_key,
|
|
|
|
&(AuReinterpretCast<PublicECCImpl>(partnerPublic)->GetKey()),
|
|
|
|
sharedKey.writePtr,
|
|
|
|
&actualSize);
|
2021-09-21 01:54:47 +00:00
|
|
|
if (ret != CRYPT_OK)
|
|
|
|
{
|
|
|
|
SysPushErrorCrypt("{}", ret);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-19 05:42:39 +00:00
|
|
|
sharedKey.writePtr += actualSize;
|
2021-09-21 01:54:47 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-20 16:37:22 +00:00
|
|
|
bool PrivateECCImpl::AsPublicECC(AuByteBuffer &out)
|
2021-09-21 01:54:47 +00:00
|
|
|
{
|
2022-09-19 05:42:39 +00:00
|
|
|
return ExportECCKey(this->_key, true, out);
|
2021-09-21 01:54:47 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 16:37:22 +00:00
|
|
|
bool PrivateECCImpl::AsPrivateECC(AuByteBuffer &out)
|
2021-09-21 01:54:47 +00:00
|
|
|
{
|
2022-09-19 05:42:39 +00:00
|
|
|
return ExportECCKey(this->_key, false, out);
|
2021-09-21 01:54:47 +00:00
|
|
|
}
|
|
|
|
}
|