AuroraRuntime/Source/Crypto/ECC/PublicECCImpl.cpp

117 lines
2.8 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 "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;
}
2022-01-20 16:37:22 +00:00
bool PublicECCImpl::Verify(const AuMemoryViewRead &hash,
const AuMemoryViewRead &signature)
2021-09-21 01:54:47 +00:00
{
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;
}
2022-01-20 16:37:22 +00:00
bool PublicECCImpl::Verify(const AuMemoryViewRead &plaintext,
const AuMemoryViewRead &signature,
2021-09-21 01:54:47 +00:00
EHashType method)
{
if (!plaintext.HasMemory())
{
SysPushErrorParam();
return {};
}
if (!signature.HasMemory())
{
SysPushErrorParam();
return {};
}
int hash = HashMethodToId(method);
if (hash == 0xFF)
{
SysPushErrorCrypt("invalid hash {}", method);
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 Verify({hashVec}, signature);
}
2022-01-20 16:37:22 +00:00
bool PublicECCImpl::AsPublicECC(AuByteBuffer &out)
2021-09-21 01:54:47 +00:00
{
return Export(true, out);
}
bool PublicECCImpl::Export(bool pub, DerBuffer &out)
{
return ExportECCKey(_key, pub, out);
}
const ecc_key &PublicECCImpl::GetKey()
{
return _key;
}
}