151 lines
3.6 KiB
C++
151 lines
3.6 KiB
C++
|
/***
|
||
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
||
|
|
||
|
File: ECCx25519Public.cpp
|
||
|
Date: 2021-9-17
|
||
|
Author: Reece
|
||
|
***/
|
||
|
#include <Source/RuntimeInternal.hpp>
|
||
|
#include "ECC.hpp"
|
||
|
#include "ECCx25519Public.hpp"
|
||
|
|
||
|
namespace Aurora::Crypto::ECC
|
||
|
{
|
||
|
PublicCurve25519Impl::PublicCurve25519Impl(bool isX25519, curve25519_key &&key) : key_(key), isX25519_(isX25519)
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
PublicCurve25519Impl::~PublicCurve25519Impl()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
bool PublicCurve25519Impl::Verify(const Memory::MemoryViewRead &hash,
|
||
|
const Memory::MemoryViewRead &signature)
|
||
|
{
|
||
|
int ok = 0;
|
||
|
|
||
|
if (this->isX25519_)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if (!hash.HasMemory())
|
||
|
{
|
||
|
SysPushErrorParam();
|
||
|
return {};
|
||
|
}
|
||
|
|
||
|
if (!signature.HasMemory())
|
||
|
{
|
||
|
SysPushErrorParam();
|
||
|
return {};
|
||
|
}
|
||
|
|
||
|
auto ret = ed25519_verify(reinterpret_cast<const unsigned char *>(hash.ptr), hash.length,
|
||
|
reinterpret_cast<const unsigned char *>(signature.ptr), signature.length,
|
||
|
&ok, &key_);
|
||
|
if (ret != CRYPT_OK)
|
||
|
{
|
||
|
SysPushErrorCrypt("{}", ret);
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return ok == 1;
|
||
|
}
|
||
|
|
||
|
bool PublicCurve25519Impl::Verify(const Memory::MemoryViewRead &plaintext,
|
||
|
const Memory::MemoryViewRead &signature,
|
||
|
EHashType method)
|
||
|
{
|
||
|
if (this->isX25519_)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if (!plaintext.HasMemory())
|
||
|
{
|
||
|
SysPushErrorParam();
|
||
|
return {};
|
||
|
}
|
||
|
|
||
|
if (!signature.HasMemory())
|
||
|
{
|
||
|
SysPushErrorParam();
|
||
|
return {};
|
||
|
}
|
||
|
|
||
|
int hash = HashMethodToId(method);
|
||
|
if (hash == 0xFF)
|
||
|
{
|
||
|
SysPushErrorCrypt("invalid hash {}", method);
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
AuByteBuffer hashVec;
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
bool PublicCurve25519Impl::AsPublicECC(Memory::ByteBuffer &out)
|
||
|
{
|
||
|
if (!AuTryResize(out, 4096))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
unsigned long actualSize;
|
||
|
int ret;
|
||
|
|
||
|
if (this->isX25519_)
|
||
|
{
|
||
|
actualSize = out.size();
|
||
|
ret = x25519_export(out.data(), &actualSize, PK_PUBLIC, &this->key_);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
actualSize = out.size();
|
||
|
ret = ed25519_export(out.data(), &actualSize, PK_PUBLIC, &this->key_);
|
||
|
}
|
||
|
|
||
|
if (ret != CRYPT_OK)
|
||
|
{
|
||
|
SysPushErrorCrypt("{}", ret);
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if (!AuTryResize(out, actualSize))
|
||
|
{
|
||
|
SysPushErrorMem();
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
EECCCurve PublicCurve25519Impl::GetType()
|
||
|
{
|
||
|
return this->isX25519_ ? EECCCurve::eCurveX25519 : EECCCurve::eCurveEd25519;
|
||
|
}
|
||
|
|
||
|
const curve25519_key &PublicCurve25519Impl::GetKey()
|
||
|
{
|
||
|
return this->key_;
|
||
|
}
|
||
|
}
|