AuroraRuntime/Source/Crypto/ECC/PrivateECCImpl.cpp

161 lines
4.1 KiB
C++
Raw Normal View History

2021-09-21 01:54:47 +00:00
/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: ECCGeneric.cpp
Date: 2021-9-17
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
{
[+] Network + Protocol + TLS - Initial Commit ============================================================================= Network ]==================================================================== ============================================================================= [+] Added (very) early Aurora::IO::Net implementation [+] AuNet::EHostnameType [+] AuNet::EIPProtocol [+] AuNet::ENetworkError [+] AuNet::ETransportProtocol [+] AuNet::INetInterface [+] AuNet::INetSrvDatagram [+] AuNet::INetSrvResolve [+] AuNet::INetSrvSockets [+] AuNet::INetSrvWorkers [+] AuNet::INetWorker [+] AuNet::IPAddress [+] AuNet::IResolver [+] AuNet::ISocket [+] AuNet::IResolver [+] AuNet::ISocketBase [+] AuNet::ISocketChannel [+] AuNet::ISocketDriver [+] AuNet::ISocketDriverFactory [+] AuNet::ISocketServer [+] AuNet::ISocketServerDriver [+] AuNet::NetEndpoint [+] AuNet::NetError [+] AuNet::NetHostname (+implementation) ============================================================================= Protocol ]=================================================================== ============================================================================= [+] IProtocolInterceptor [+] IProtocolInterceptorEx [+] IProtocolStack (+implementation) ============================================================================= TLS ]======================================================================== ============================================================================= [+] ITLSContext [+] TLSProtocolRecv [+] TLSProtocolSend (+implementation) ============================================================================= IO Bug Fixes ]=============================================================== ============================================================================= [*] IOProcessor::SubmitIOWorkItem should signal the CvEvent, forcing at least once future tick (wont optimize with if in tick & not yet dispatched work items) [*] Split IOPipeWork in into IOPipeProcessor header [+] IOPipeWork::GetBuffer (internal reallocation) [*] Harden against IAsyncTransactions without a loop source [*] Missing null `if (processor->listener)` in IOProcessor [*] Solved some soft-lock conditions under Linux's LoopQueue (added deferred commits) [*] Quick hack: IOProcessor::HasItems() should OR the early can-tick check function. ============================================================================= Other ]====================================================================== ============================================================================= [+] Linux: LSSignalCatcher [+] `static void AuResetMember(Aurora::Memory::ByteBuffer &ref)` for AuROXTL [*] Attempt to enforce a normalization and don't overwrite-readptr-under-istreamwriters policy in ByteBuffer_ReadWrite (circular buffers) [*] Bad ECC ctors ============================================================================= Known issues ]=============================================================== ============================================================================= > Linux net is nowhere near done > UDP socket emulation layer isn't implemented > Ciphersuite API is a stub > Private key API is a stub > ...therefore no TLS servers > Missing thread safety precautions under net > Net implementation is still beri early
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()
{
ecc_free(&_key);
}
EECCCurve PrivateECCImpl::GetType()
{
return _type;
}
2022-01-20 16:37:22 +00:00
bool PrivateECCImpl::Sign(const AuMemoryViewRead &plainText,
2021-09-21 01:54:47 +00:00
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 {};
}
int hash = HashMethodToId(method);
if (hash == 0xFF)
{
SysPushErrorCrypt("invalid hash {}", method);
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();
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)
{
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
{
prng_state yarrow_prng;
if (!hash.HasMemory())
{
SysPushErrorParam();
return {};
}
if (!AuTryResize(out, 1024))
{
SysPushErrorMem();
return false;
}
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);
if (ret != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
return false;
}
if (!AuTryResize(out, len))
{
SysPushErrorMem();
return false;
}
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-01-20 16:37:22 +00:00
AuByteBuffer sharedSecret;
2021-09-21 01:54:47 +00:00
if (!AuTryResize(sharedSecret, 128))
{
SysPushErrorMem();
return false;
}
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);
2021-09-21 01:54:47 +00:00
if (ret != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
return false;
}
if (!AuTryResize(sharedSecret, actualSize))
{
SysPushErrorMem();
return false;
}
return true;
}
2022-01-20 16:37:22 +00:00
bool PrivateECCImpl::AsPublicECC(AuByteBuffer &out)
2021-09-21 01:54:47 +00:00
{
return ExportECCKey(_key, true, out);
}
2022-01-20 16:37:22 +00:00
bool PrivateECCImpl::AsPrivateECC(AuByteBuffer &out)
2021-09-21 01:54:47 +00:00
{
return ExportECCKey(_key, false, out);
}
}