crypto: export EC JWK coordinates directly · nodejs/node@ee956ae · GitHub
Skip to content

Commit ee956ae

Browse files
panvaaduh95
authored andcommitted
crypto: export EC JWK coordinates directly
Query provider coordinates together instead of serializing and decoding the public point. 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 684008b commit ee956ae

4 files changed

Lines changed: 96 additions & 23 deletions

File tree

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

Lines changed: 19 additions & 0 deletions

‎deps/ncrypto/ncrypto.cc‎

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

6919+
bool Ec::GetKeyComponents(const EVPKeyPointer& key,
6920+
BignumPointer* x,
6921+
BignumPointer* y,
6922+
BignumPointer* priv,
6923+
int* degree) {
6924+
#if NCRYPTO_USE_OPENSSL3_PROVIDER
6925+
const int nid = GetCurveId(key);
6926+
switch (nid) {
6927+
case NID_X9_62_prime256v1:
6928+
case NID_secp256k1:
6929+
*degree = 256;
6930+
break;
6931+
case NID_secp384r1:
6932+
*degree = 384;
6933+
break;
6934+
case NID_secp521r1:
6935+
*degree = 521;
6936+
break;
6937+
default:
6938+
*degree = 0;
6939+
}
6940+
if (*degree != 0) {
6941+
MarkPopErrorOnReturn pop_errors;
6942+
unsigned char x_bytes[66]{};
6943+
unsigned char y_bytes[66]{};
6944+
const size_t width = (*degree + 7) / 8;
6945+
OSSL_PARAM params[] = {
6946+
OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_EC_PUB_X, x_bytes, width),
6947+
OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_EC_PUB_Y, y_bytes, width),
6948+
OSSL_PARAM_construct_end(),
6949+
};
6950+
if (EVP_PKEY_get_params(key.get(), params) == 1 &&
6951+
OSSL_PARAM_modified(&params[0]) && OSSL_PARAM_modified(&params[1])) {
6952+
x->reset(BN_native2bn(x_bytes, width, nullptr));
6953+
y->reset(BN_native2bn(y_bytes, width, nullptr));
6954+
return *x && *y &&
6955+
(priv == nullptr ||
6956+
GetPKeyBnParam(key.get(), OSSL_PKEY_PARAM_PRIV_KEY, priv));
6957+
}
6958+
}
6959+
#endif
6960+
ECKeyPointer ec(key);
6961+
if (!ec || ec.getPublicKey() == nullptr) return false;
6962+
*degree = EC_GROUP_get_degree(ec.getGroup());
6963+
x->reset(BN_new());
6964+
y->reset(BN_new());
6965+
if (!*x || !*y ||
6966+
EC_POINT_get_affine_coordinates(
6967+
ec.getGroup(), ec.getPublicKey(), x->get(), y->get(), nullptr) != 1) {
6968+
return false;
6969+
}
6970+
if (priv != nullptr) {
6971+
if (ec.getPrivateKey() == nullptr) return false;
6972+
priv->reset(BN_dup(ec.getPrivateKey()));
6973+
if (!*priv) return false;
6974+
}
6975+
return true;
6976+
}
6977+
69196978
int Ec::GetCurveId(const EVPKeyPointer& key) {
69206979
#if NCRYPTO_USE_OPENSSL3_PROVIDER
69216980
char name[80];

‎deps/ncrypto/ncrypto.h‎

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

814814
static int GetCurveIdFromName(const char* name);
815815
static int GetCurveId(const EVPKeyPointer& key);
816+
static bool GetKeyComponents(const EVPKeyPointer& key,
817+
BignumPointer* x,
818+
BignumPointer* y,
819+
BignumPointer* priv,
820+
int* degree);
816821

817822
using GetCurveCallback = std::function<bool(const char*)>;
818823
static bool GetCurves(GetCurveCallback callback);

‎src/crypto/crypto_ec.cc‎

Lines changed: 13 additions & 23 deletions

0 commit comments

Comments
 (0)