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

Commit 775173a

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 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 28acafe commit 775173a

4 files changed

Lines changed: 103 additions & 2 deletions

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
@@ -1764,6 +1764,7 @@ class ECKeyPointer final {
17641764
bool setPublicKeyRaw(const BignumPointer& x, const BignumPointer& y);
17651765
bool generate();
17661766
bool checkKey() const;
1767+
bool checkPrivateKey() const;
17671768
DataPointer computeSecret(const ECPointPointer& peer) const;
17681769

17691770
const EC_GROUP* getGroup() const;

‎src/crypto/crypto_ec.cc‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -756,15 +756,18 @@ KeyObjectData ImportJWKEcKey(Environment* env, Local<Object> jwk) {
756756
return {};
757757
}
758758
// Verify that the public point matches the private scalar (d*G == (x,y)).
759-
if (!ec.checkKey()) {
759+
if (!ec.checkPrivateKey()) {
760760
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK EC key");
761761
return {};
762762
}
763763
}
764764

765765
auto pkey = EVPKeyPointer::New();
766766
if (!pkey) return {};
767-
CHECK(pkey.set(ec));
767+
if (!pkey.set(ec)) {
768+
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK EC key");
769+
return {};
770+
}
768771

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

0 commit comments

Comments
 (0)