crypto: optimize private EC JWK import · nodejs/node@5c51129 · GitHub
Skip to content

Commit 5c51129

Browse files
panvaaduh95
authored andcommitted
crypto: optimize private EC JWK import
Avoid creating temporary EVP objects and repeating key validation while retaining the private scalar range and public/private consistency checks. Assisted-by: GitHub Copilot Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #65908 Backport-PR-URL: #66233 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 3f0b6d3 commit 5c51129

4 files changed

Lines changed: 107 additions & 1 deletion

File tree

‎deps/ncrypto/ncrypto.cc‎

Lines changed: 18 additions & 0 deletions

‎deps/ncrypto/ncrypto.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,6 +1773,7 @@ class ECKeyPointer final {
17731773
bool setPublicKeyRaw(const BignumPointer& x, const BignumPointer& y);
17741774
bool generate();
17751775
bool checkKey() const;
1776+
bool checkPrivateKey() const;
17761777
DataPointer computeSecret(const ECPointPointer& peer) const;
17771778

17781779
const EC_GROUP* getGroup() const;

‎src/crypto/crypto_ec.cc‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,11 +870,19 @@ KeyObjectData ImportJWKEcKey(Environment* env, Local<Object> jwk) {
870870
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK EC key");
871871
return {};
872872
}
873+
// Verify that the public point matches the private scalar (d*G == (x,y)).
874+
if (!ec.checkPrivateKey()) {
875+
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK EC key");
876+
return {};
877+
}
873878
}
874879

875880
auto pkey = EVPKeyPointer::New();
876881
if (!pkey) return {};
877-
CHECK(pkey.set(ec));
882+
if (!pkey.set(ec)) {
883+
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK EC key");
884+
return {};
885+
}
878886

879887
return KeyObjectData::CreateAsymmetric(type, std::move(pkey));
880888
}
Lines changed: 79 additions & 0 deletions

0 commit comments

Comments
 (0)