Skip to content

Commit b100101

Browse files
committed
src: move curve utility methods to ncrypto
1 parent ba318d2 commit b100101

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

deps/ncrypto/ncrypto.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3753,6 +3753,26 @@ int Ec::getCurve() const {
37533753
return EC_GROUP_get_curve_name(getGroup());
37543754
}
37553755

3756+
int Ec::GetCurveIdFromName(std::string_view name) {
3757+
int nid = EC_curve_nist2nid(name.data());
3758+
if (nid == NID_undef) {
3759+
nid = OBJ_sn2nid(name.data());
3760+
}
3761+
return nid;
3762+
}
3763+
3764+
bool Ec::GetCurves(Ec::GetCurveCallback callback) {
3765+
const size_t count = EC_get_builtin_curves(nullptr, 0);
3766+
std::vector<EC_builtin_curve> curves(count);
3767+
if (EC_get_builtin_curves(curves.data(), count) != count) {
3768+
return false;
3769+
}
3770+
for (auto curve : curves) {
3771+
if (!callback(OBJ_nid2sn(curve.nid))) return false;
3772+
}
3773+
return true;
3774+
}
3775+
37563776
// ============================================================================
37573777

37583778
EVPMDCtxPointer::EVPMDCtxPointer() : ctx_(nullptr) {}

deps/ncrypto/ncrypto.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,11 @@ class Ec final {
397397
inline operator bool() const { return ec_ != nullptr; }
398398
inline operator OSSL3_CONST EC_KEY*() const { return ec_; }
399399

400+
static int GetCurveIdFromName(std::string_view name);
401+
402+
using GetCurveCallback = std::function<bool(std::string_view)>;
403+
static bool GetCurves(GetCurveCallback callback);
404+
400405
private:
401406
OSSL3_CONST EC_KEY* ec_ = nullptr;
402407
};

src/crypto/crypto_ec.cc

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace node {
2020

2121
using ncrypto::BignumPointer;
2222
using ncrypto::DataPointer;
23+
using ncrypto::Ec;
2324
using ncrypto::ECGroupPointer;
2425
using ncrypto::ECKeyPointer;
2526
using ncrypto::ECPointPointer;
@@ -47,13 +48,6 @@ using v8::Value;
4748

4849
namespace crypto {
4950

50-
int GetCurveFromName(const char* name) {
51-
int nid = EC_curve_nist2nid(name);
52-
if (nid == NID_undef)
53-
nid = OBJ_sn2nid(name);
54-
return nid;
55-
}
56-
5751
void ECDH::Initialize(Environment* env, Local<Object> target) {
5852
Isolate* isolate = env->isolate();
5953
Local<Context> context = env->context();
@@ -100,13 +94,10 @@ void ECDH::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
10094

10195
void ECDH::GetCurves(const FunctionCallbackInfo<Value>& args) {
10296
Environment* env = Environment::GetCurrent(args);
103-
const size_t num_curves = EC_get_builtin_curves(nullptr, 0);
104-
std::vector<EC_builtin_curve> curves(num_curves);
105-
CHECK_EQ(EC_get_builtin_curves(curves.data(), num_curves), num_curves);
106-
107-
LocalVector<Value> arr(env->isolate(), num_curves);
108-
std::transform(curves.begin(), curves.end(), arr.begin(), [env](auto& curve) {
109-
return OneByteString(env->isolate(), OBJ_nid2sn(curve.nid));
97+
LocalVector<Value> arr(env->isolate());
98+
Ec::GetCurves([&](std::string_view curve) -> bool {
99+
arr.push_back(OneByteString(env->isolate(), curve));
100+
return true;
110101
});
111102
args.GetReturnValue().Set(Array::New(env->isolate(), arr.data(), arr.size()));
112103
}
@@ -548,7 +539,7 @@ Maybe<void> EcKeyGenTraits::AdditionalConfig(
548539
CHECK(args[*offset + 1]->IsInt32()); // param encoding
549540

550541
Utf8Value curve_name(env->isolate(), args[*offset]);
551-
params->params.curve_nid = GetCurveFromName(*curve_name);
542+
params->params.curve_nid = Ec::GetCurveIdFromName(curve_name.ToStringView());
552543
if (params->params.curve_nid == NID_undef) {
553544
THROW_ERR_CRYPTO_INVALID_CURVE(env);
554545
return Nothing<void>();
@@ -839,7 +830,7 @@ KeyObjectData ImportJWKEcKey(Environment* env,
839830
CHECK(args[offset]->IsString()); // curve name
840831
Utf8Value curve(env->isolate(), args[offset].As<String>());
841832

842-
int nid = GetCurveFromName(*curve);
833+
int nid = Ec::GetCurveIdFromName(curve.ToStringView());
843834
if (nid == NID_undef) { // Unknown curve
844835
THROW_ERR_CRYPTO_INVALID_CURVE(env);
845836
return {};

src/crypto/crypto_ec.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
namespace node {
1717
namespace crypto {
18-
int GetCurveFromName(const char* name);
1918

2019
class ECDH final : public BaseObject {
2120
public:

0 commit comments

Comments
 (0)