/*** Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. File: ECCGeneric.cpp Date: 2021-9-17 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(&_key); } EECCCurve PublicECCImpl::GetType() { return _type; } bool PublicECCImpl::Verify(const Memory::MemoryViewRead &hash, const Memory::MemoryViewRead &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 Memory::MemoryViewRead &plaintext, const Memory::MemoryViewRead &signature, 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; } Memory::ByteBuffer hashVec; if (!AuTryResize(hashVec, 128)) { SysPushErrorMem(); return false; } unsigned long hashSize = hashVec.size(); auto ret = hash_memory(hash, reinterpret_cast(plaintext.ptr), plaintext.length, reinterpret_cast(hashVec.data()), &hashSize); if (ret != CRYPT_OK) { SysPushErrorCrypt("{}", ret); return false; } return Verify({hashVec}, signature); } bool PublicECCImpl::AsPublicECC(Memory::ByteBuffer &out) { return Export(true, out); } bool PublicECCImpl::Export(bool pub, DerBuffer &out) { return ExportECCKey(_key, pub, out); } const ecc_key &PublicECCImpl::GetKey() { return _key; } }