Skip to content

Commit

Permalink
src: refactor ECDHBitsJob signature
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Oct 30, 2024
1 parent 6dea41d commit e9b3c67
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 27 deletions.
1 change: 0 additions & 1 deletion lib/internal/crypto/diffiehellman.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ async function ecdhDeriveBits(algorithm, baseKey, length) {

const bits = await jobPromise(() => new ECDHBitsJob(
kCryptoJobAsync,
key.algorithm.name === 'ECDH' ? baseKey.algorithm.namedCurve : baseKey.algorithm.name,
key[kKeyObject][kHandle],
baseKey[kKeyObject][kHandle]));

Expand Down
30 changes: 5 additions & 25 deletions src/crypto/crypto_ec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,6 @@ int GetCurveFromName(const char* name) {
return nid;
}

int GetOKPCurveFromName(const char* name) {
int nid;
if (strcmp(name, "Ed25519") == 0) {
nid = EVP_PKEY_ED25519;
} else if (strcmp(name, "Ed448") == 0) {
nid = EVP_PKEY_ED448;
} else if (strcmp(name, "X25519") == 0) {
nid = EVP_PKEY_X25519;
} else if (strcmp(name, "X448") == 0) {
nid = EVP_PKEY_X448;
} else {
nid = NID_undef;
}
return nid;
}

void ECDH::Initialize(Environment* env, Local<Object> target) {
Isolate* isolate = env->isolate();
Local<Context> context = env->context();
Expand Down Expand Up @@ -450,25 +434,21 @@ Maybe<void> ECDHBitsTraits::AdditionalConfig(
ECDHBitsConfig* params) {
Environment* env = Environment::GetCurrent(args);

CHECK(args[offset]->IsString()); // curve name
CHECK(args[offset + 1]->IsObject()); // public key
CHECK(args[offset + 2]->IsObject()); // private key
CHECK(args[offset]->IsObject()); // public key
CHECK(args[offset + 1]->IsObject()); // private key

KeyObjectHandle* private_key;
KeyObjectHandle* public_key;

Utf8Value name(env->isolate(), args[offset]);

ASSIGN_OR_RETURN_UNWRAP(&public_key, args[offset + 1], Nothing<void>());
ASSIGN_OR_RETURN_UNWRAP(&private_key, args[offset + 2], Nothing<void>());
ASSIGN_OR_RETURN_UNWRAP(&public_key, args[offset], Nothing<void>());
ASSIGN_OR_RETURN_UNWRAP(&private_key, args[offset + 1], Nothing<void>());

if (private_key->Data().GetKeyType() != kKeyTypePrivate ||
public_key->Data().GetKeyType() != kKeyTypePublic) {
THROW_ERR_CRYPTO_INVALID_KEYTYPE(env);
return Nothing<void>();
}

params->id_ = GetOKPCurveFromName(*name);
params->private_ = private_key->Data().addRef();
params->public_ = public_key->Data().addRef();

Expand All @@ -482,7 +462,7 @@ bool ECDHBitsTraits::DeriveBits(Environment* env,
const auto& m_privkey = params.private_.GetAsymmetricKey();
const auto& m_pubkey = params.public_.GetAsymmetricKey();

switch (params.id_) {
switch (m_privkey.id()) {
case EVP_PKEY_X25519:
// Fall through
case EVP_PKEY_X448: {
Expand Down
1 change: 0 additions & 1 deletion src/crypto/crypto_ec.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
namespace node {
namespace crypto {
int GetCurveFromName(const char* name);
int GetOKPCurveFromName(const char* name);

class ECDH final : public BaseObject {
public:
Expand Down
16 changes: 16 additions & 0 deletions src/crypto/crypto_keys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,22 @@ void KeyObjectHandle::InitECRaw(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(true);
}

int GetOKPCurveFromName(const char* name) {
int nid;
if (strcmp(name, "Ed25519") == 0) {
nid = EVP_PKEY_ED25519;
} else if (strcmp(name, "Ed448") == 0) {
nid = EVP_PKEY_ED448;
} else if (strcmp(name, "X25519") == 0) {
nid = EVP_PKEY_X25519;
} else if (strcmp(name, "X448") == 0) {
nid = EVP_PKEY_X448;
} else {
nid = NID_undef;
}
return nid;
}

void KeyObjectHandle::InitEDRaw(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
KeyObjectHandle* key;
Expand Down
2 changes: 2 additions & 0 deletions src/crypto/crypto_keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ WebCryptoKeyExportStatus PKEY_SPKI_Export(const KeyObjectData& key_data,
WebCryptoKeyExportStatus PKEY_PKCS8_Export(const KeyObjectData& key_data,
ByteSource* out);

int GetOKPCurveFromName(const char* name);

namespace Keys {
void Initialize(Environment* env, v8::Local<v8::Object> target);
void RegisterExternalReferences(ExternalReferenceRegistry* registry);
Expand Down

0 comments on commit e9b3c67

Please sign in to comment.