AuroraRuntime/Source/Crypto/ECC/PublicECCImpl.cpp
2024-02-16 13:07:28 +00:00

115 lines
2.8 KiB
C++

/***
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>
#include "ECC.hpp"
#include "ECCGeneric.hpp"
#include "ECCCurves.hpp"
#include "PublicECCImpl.hpp"
namespace Aurora::Crypto::ECC
{
PublicECCImpl::PublicECCImpl(EECCCurve type, ecc_key &key) : _key(key), _type(type)
{
}
PublicECCImpl::~PublicECCImpl()
{
ecc_free(&_key);
}
EECCCurve PublicECCImpl::GetType()
{
return _type;
}
bool PublicECCImpl::Verify(const AuMemoryViewRead &hash,
const AuMemoryViewRead &signature)
{
int ok = 0;
if (!hash.HasMemory())
{
SysPushErrorParam();
return {};
}
if (!signature.HasMemory())
{
SysPushErrorParam();
return {};
}
auto ret = ecc_verify_hash_ex(reinterpret_cast<const unsigned char *>(hash.ptr), hash.length,
reinterpret_cast<const unsigned char *>(signature.ptr), signature.length,
LTC_ECCSIG_ETH27, &ok, &_key);
if (ret != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
return false;
}
return ok == 1;
}
bool PublicECCImpl::Verify(const AuMemoryViewRead &plaintext,
const AuMemoryViewRead &signature,
AuHashing::EHashType method)
{
if (!plaintext.HasMemory())
{
SysPushErrorParam();
return {};
}
if (!signature.HasMemory())
{
SysPushErrorParam();
return {};
}
int hash = ::Crypto::HashMethodToId(method);
if (hash == 0xFF)
{
SysPushErrorCrypt("invalid hash {}", AuUInt(method));
return false;
}
AuByteBuffer hashVec;
if (!AuTryResize(hashVec, 128))
{
SysPushErrorMem();
return false;
}
unsigned long hashSize = hashVec.size();
auto iRet = ::hash_memory(hash,
AuReinterpretCast<const unsigned char *>(plaintext.ptr), plaintext.length,
AuReinterpretCast<unsigned char *>(hashVec.data()), &hashSize);
if (iRet != CRYPT_OK)
{
SysPushErrorCrypt("{}", iRet);
return false;
}
return Verify({hashVec}, signature);
}
bool PublicECCImpl::AsPublicECC(AuByteBuffer &out)
{
return ExportECCKey(_key, true, out);
}
const ecc_key &PublicECCImpl::GetKey()
{
return _key;
}
}