AuroraRuntime/Source/Crypto/ECC/ECCX25519Public.cpp
Reece Wilson 033f7e2453 [+] Aurora::Crypto::X509::CertRequest
[+] Aurora::Crypto::X509::GenerateCertificate
[*] Fix lazily copied gen1 RSA code
[+] Aurora::Crypto::ECC::EECCCurve::eCurveSECP256R1
[+] Aurora::Crypto::ECC::EECCCurve::eCurveSECP256K1
[+] Aurora::Crypto::ECC::EECCCurve::eCurveSECP384R1
[+] Aurora::Crypto::ECC::EECCCurve::eCurveSECP521R1
[*] Unfuck ECC interop
[*] Tls pinning: use mbedtls_ssl_conf_verify for tls1.3 (when mbedtls is in a better state)
2022-11-18 21:03:11 +00:00

149 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), bIsX25519_(isX25519)
{
}
PublicCurve25519Impl::~PublicCurve25519Impl()
{
}
bool PublicCurve25519Impl::Verify(const Memory::MemoryViewRead &hash,
const Memory::MemoryViewRead &signature)
{
int ok = 0;
if (this->bIsX25519_)
{
return false;
}
if (!hash.HasMemory())
{
SysPushErrorParam();
return {};
}
if (!signature.HasMemory())
{
SysPushErrorParam();
return {};
}
auto iRet = ed25519_verify(AuReinterpretCast<const unsigned char *>(hash.ptr), hash.length,
AuReinterpretCast<const unsigned char *>(signature.ptr), signature.length,
&ok, &key_);
if (iRet != CRYPT_OK)
{
SysPushErrorCrypt("{}", iRet);
return false;
}
return ok == 1;
}
bool PublicCurve25519Impl::Verify(const Memory::MemoryViewRead &plaintext,
const Memory::MemoryViewRead &signature,
AuHashing::EHashType method)
{
if (this->bIsX25519_)
{
return false;
}
if (!plaintext.HasMemory())
{
SysPushErrorParam();
return {};
}
if (!signature.HasMemory())
{
SysPushErrorParam();
return {};
}
int hash = ::Crypto::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 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 PublicCurve25519Impl::AsPublicECC(Memory::ByteBuffer &out)
{
auto writeView = out.GetOrAllocateLinearWriteable(4096);
if (!writeView)
{
SysPushErrorMem();
return {};
}
unsigned long actualSize;
int ret;
actualSize = 4096;
if (this->bIsX25519_)
{
ret = ::x25519_export(out.writePtr, &actualSize, PK_PUBLIC | PK_STD, &this->key_);
}
else
{
ret = ::ed25519_export(out.writePtr, &actualSize, PK_PUBLIC | PK_STD, &this->key_);
}
if (ret != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
return false;
}
out.writePtr += actualSize;
return true;
}
EECCCurve PublicCurve25519Impl::GetType()
{
return this->bIsX25519_ ? EECCCurve::eCurveX25519 : EECCCurve::eCurveEd25519;
}
const curve25519_key &PublicCurve25519Impl::GetKey()
{
return this->key_;
}
}