/*** 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 #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(&this->_key); AuMemset(&this->_key, 'N', AuSizeOf(this->_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(hash.ptr), hash.length, reinterpret_cast(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(plaintext.ptr), plaintext.length, AuReinterpretCast(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; } }