AuroraRuntime/Source/Crypto/ECC/ECCGeneric.hpp
2024-02-16 13:07:28 +00:00

91 lines
2.5 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: ECCGeneric.hpp
Date: 2021-9-17
Author: Reece
***/
#pragma once
#include "ECCCurves.hpp"
namespace Aurora::Crypto::ECC
{
template<typename Type_t>
Type_t *NewECC(EECCCurve curve, const Memory::ByteBuffer &pub);
bool ExportECCKey(const ecc_key &key, bool pub, DerBuffer &out);
int pk_oid_str_to_num(const char *OID, unsigned long *oid, unsigned long *oidlen);
int pk_oid_num_to_str(const unsigned long *oid, unsigned long oidlen, char *OID, unsigned long *outlen);
template<typename Type_t>
static Type_t *NewStdECC(EECCCurve curve, const Memory::MemoryViewRead &pk, bool cert = false)
{
ecc_key in {};
int ret {};
AuOptional<const ltc_ecc_curve *> ref;
//if (EECCCurveIsValid(curve))
//{
// ref = GetECCCurve(curve);
// if (!ref)
// {
// SysPushErrorParam("This curve isn't supported here");
// // wrong function, bucko
// return nullptr;
// }
//}
if (cert)
{
ret = ecc_import_x509(pk.Begin<const unsigned char>(), pk.length, &in);
}
else
{
ret = ecc_import_openssl(pk.Begin<const unsigned char>(), pk.length, &in);
}
if (ret != CRYPT_OK)
{
//SysPushErrorCrypto("{}", ret);
return nullptr;
}
if (ref)
{
unsigned long oid[16] {};
unsigned long oidLength = AuArraySize(oid);
pk_oid_str_to_num(ref.value()->OID, oid, &oidLength);
if (oidLength != in.dp.oidlen ||
AuMemcmp(in.dp.oid, oid, in.dp.oidlen * sizeof(unsigned long)))
{
SysPushErrorParam("Improper curve type, expected {}, got {}, for ECCCurveType: {}", ref.value()->OID, AuList<unsigned long>(in.dp.oid, in.dp.oid + in.dp.oidlen), (AuUInt)curve);
ecc_free(&in);
return nullptr;
}
}
else
{
curve = OIDToCurve(in.dp.oid, in.dp.oidlen);
}
//if (!EECCCurveIsValid(curve))
//{
// SysPushErrorCrypt("Couldn't find curve");
// return {};
//}
Type_t *out = _new Type_t(curve, in);
if (!out)
{
ecc_free(&in);
}
return out;
}
AuOptional<IECCPrivate *> GenerateNewGenericECC(EECCCurve curve);
}