crypto: avoid EC raw export reconstruction · nodejs/node@5b592b7 · GitHub
Skip to content

Commit 5b592b7

Browse files
panvaaduh95
authored andcommitted
crypto: avoid EC raw export reconstruction
Read private scalars and matching uncompressed provider encodings directly. 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 ee956ae commit 5b592b7

4 files changed

Lines changed: 95 additions & 52 deletions

File tree

‎benchmark/crypto/ec-raw-export.js‎

Lines changed: 23 additions & 0 deletions

‎deps/ncrypto/ncrypto.cc‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6916,6 +6916,51 @@ int Ec::getCurve() const {
69166916
return EC_GROUP_get_curve_name(getGroup());
69176917
}
69186918

6919+
DataPointer Ec::TryExportPublic(const EVPKeyPointer& key,
6920+
point_conversion_form_t form) {
6921+
if (form != POINT_CONVERSION_UNCOMPRESSED) return {};
6922+
#if NCRYPTO_USE_OPENSSL3_PROVIDER
6923+
{
6924+
MarkPopErrorOnReturn pop_errors;
6925+
size_t length = 0;
6926+
if (EVP_PKEY_get_octet_string_param(
6927+
key.get(), OSSL_PKEY_PARAM_PUB_KEY, nullptr, 0, &length) == 1) {
6928+
auto bytes = DataPointer::Alloc(length);
6929+
if (bytes && length != 0 &&
6930+
EVP_PKEY_get_octet_string_param(key.get(),
6931+
OSSL_PKEY_PARAM_PUB_KEY,
6932+
bytes.get<unsigned char>(),
6933+
length,
6934+
&length) == 1 &&
6935+
(bytes.get<unsigned char>()[0] & ~1) == form) {
6936+
return bytes.resize(length);
6937+
}
6938+
}
6939+
}
6940+
#endif
6941+
return {};
6942+
}
6943+
6944+
DataPointer Ec::ExportPrivate(const EVPKeyPointer& key) {
6945+
#if NCRYPTO_USE_OPENSSL3_PROVIDER
6946+
{
6947+
MarkPopErrorOnReturn pop_errors;
6948+
BignumPointer priv;
6949+
BignumPointer order;
6950+
if (GetPKeyBnParam(key.get(), OSSL_PKEY_PARAM_PRIV_KEY, &priv) &&
6951+
GetPKeyBnParam(key.get(), OSSL_PKEY_PARAM_EC_ORDER, &order)) {
6952+
return priv.encodePadded(order.byteLength());
6953+
}
6954+
}
6955+
#endif
6956+
ECKeyPointer ec(key);
6957+
if (!ec || ec.getPrivateKey() == nullptr) return {};
6958+
auto order = BignumPointer::New();
6959+
if (!order || !EC_GROUP_get_order(ec.getGroup(), order.get(), nullptr))
6960+
return {};
6961+
return BignumPointer::EncodePadded(ec.getPrivateKey(), order.byteLength());
6962+
}
6963+
69196964
bool Ec::GetKeyComponents(const EVPKeyPointer& key,
69206965
BignumPointer* x,
69216966
BignumPointer* y,

‎deps/ncrypto/ncrypto.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,9 @@ class Ec final {
813813

814814
static int GetCurveIdFromName(const char* name);
815815
static int GetCurveId(const EVPKeyPointer& key);
816+
static DataPointer TryExportPublic(const EVPKeyPointer& key,
817+
point_conversion_form_t form);
818+
static DataPointer ExportPrivate(const EVPKeyPointer& key);
816819
static bool GetKeyComponents(const EVPKeyPointer& key,
817820
BignumPointer* x,
818821
BignumPointer* y,

‎src/crypto/crypto_keys.cc‎

Lines changed: 24 additions & 52 deletions

0 commit comments

Comments
 (0)