mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-30 21:50:47 +01:00
add some elliptic curve utility functions
This commit is contained in:
@@ -204,4 +204,54 @@ std::string ECKeyImpl::getCurveName(int nid)
|
||||
}
|
||||
|
||||
|
||||
int ECKeyImpl::getCurveNID(std::string& name)
|
||||
{
|
||||
std::string curveName;
|
||||
size_t len = EC_get_builtin_curves(NULL, 0);
|
||||
EC_builtin_curve* pCurves =
|
||||
(EC_builtin_curve*)OPENSSL_malloc(sizeof(EC_builtin_curve) * len);
|
||||
if (!pCurves) return -1;
|
||||
|
||||
if (!EC_get_builtin_curves(pCurves, len))
|
||||
{
|
||||
OPENSSL_free(pCurves);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int nid = -1;
|
||||
const int bufLen = 128;
|
||||
char buf[bufLen];
|
||||
if (name.empty())
|
||||
{
|
||||
std::memset(buf, 0, bufLen);
|
||||
OBJ_obj2txt(buf, bufLen, OBJ_nid2obj(nid), 0);
|
||||
name = buf;
|
||||
nid = pCurves[0].nid;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < len; ++i)
|
||||
{
|
||||
std::memset(buf, 0, bufLen);
|
||||
OBJ_obj2txt(buf, bufLen, OBJ_nid2obj(pCurves[i].nid), 0);
|
||||
if (strncmp(name.c_str(), buf, name.size() > bufLen ? bufLen : name.size()) == 0)
|
||||
{
|
||||
nid = pCurves[i].nid;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
OPENSSL_free(pCurves);
|
||||
return nid;
|
||||
}
|
||||
|
||||
|
||||
bool ECKeyImpl::hasCurve(const std::string& name)
|
||||
{
|
||||
std::string tmp(name);
|
||||
return (-1 != getCurveNID(tmp));
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Crypto
|
||||
|
||||
Reference in New Issue
Block a user