src: refactor ECDHBitsJob signature

PR-URL: https://github.com/nodejs/node/pull/55610
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
This commit is contained in:
Filip Skokan 2024-11-02 12:36:09 +00:00 committed by GitHub
parent d37214bb5d
commit 4bf5731fa8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 23 additions and 27 deletions

View File

@ -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]));

View File

@ -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();
@ -450,17 +434,14 @@ 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) {
@ -468,7 +449,6 @@ Maybe<void> ECDHBitsTraits::AdditionalConfig(
return Nothing<void>();
}
params->id_ = GetOKPCurveFromName(*name);
params->private_ = private_key->Data().addRef();
params->public_ = public_key->Data().addRef();
@ -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: {

View File

@ -16,7 +16,6 @@
namespace node {
namespace crypto {
int GetCurveFromName(const char* name);
int GetOKPCurveFromName(const char* name);
class ECDH final : public BaseObject {
public:

View File

@ -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;

View File

@ -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);