Skip to content

Commit c008b15

Browse files
jasnelladuh95
authored andcommitted
src: update ECGroupPointer in ncrypto
PR-URL: #56526 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 5673dc7 commit c008b15

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

deps/ncrypto/ncrypto.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2677,4 +2677,34 @@ Buffer<unsigned char> ECDSASigPointer::encode() const {
26772677
return buf;
26782678
}
26792679

2680+
// ============================================================================
2681+
2682+
ECGroupPointer::ECGroupPointer() : group_(nullptr) {}
2683+
2684+
ECGroupPointer::ECGroupPointer(EC_GROUP* group) : group_(group) {}
2685+
2686+
ECGroupPointer::ECGroupPointer(ECGroupPointer&& other) noexcept
2687+
: group_(other.release()) {}
2688+
2689+
ECGroupPointer& ECGroupPointer::operator=(ECGroupPointer&& other) noexcept {
2690+
group_.reset(other.release());
2691+
return *this;
2692+
}
2693+
2694+
ECGroupPointer::~ECGroupPointer() {
2695+
reset();
2696+
}
2697+
2698+
void ECGroupPointer::reset(EC_GROUP* group) {
2699+
group_.reset();
2700+
}
2701+
2702+
EC_GROUP* ECGroupPointer::release() {
2703+
return group_.release();
2704+
}
2705+
2706+
ECGroupPointer ECGroupPointer::NewByCurveName(int nid) {
2707+
return ECGroupPointer(EC_GROUP_new_by_curve_name(nid));
2708+
}
2709+
26802710
} // namespace ncrypto

deps/ncrypto/ncrypto.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ using DeleteFnPtr = typename FunctionDeleter<T, function>::Pointer;
197197

198198
using BignumCtxPointer = DeleteFnPtr<BN_CTX, BN_CTX_free>;
199199
using BignumGenCallbackPointer = DeleteFnPtr<BN_GENCB, BN_GENCB_free>;
200-
using ECGroupPointer = DeleteFnPtr<EC_GROUP, EC_GROUP_free>;
201200
using ECKeyPointer = DeleteFnPtr<EC_KEY, EC_KEY_free>;
202201
using ECPointPointer = DeleteFnPtr<EC_POINT, EC_POINT_free>;
203202
using EVPKeyCtxPointer = DeleteFnPtr<EVP_PKEY_CTX, EVP_PKEY_CTX_free>;
@@ -852,6 +851,28 @@ class ECDSASigPointer final {
852851
const BIGNUM* ps_ = nullptr;
853852
};
854853

854+
class ECGroupPointer final {
855+
public:
856+
explicit ECGroupPointer();
857+
explicit ECGroupPointer(EC_GROUP* group);
858+
ECGroupPointer(ECGroupPointer&& other) noexcept;
859+
ECGroupPointer& operator=(ECGroupPointer&& other) noexcept;
860+
NCRYPTO_DISALLOW_COPY(ECGroupPointer)
861+
~ECGroupPointer();
862+
863+
inline bool operator==(std::nullptr_t) noexcept { return group_ == nullptr; }
864+
inline operator bool() const { return group_ != nullptr; }
865+
inline EC_GROUP* get() const { return group_.get(); }
866+
inline operator EC_GROUP*() const { return group_.get(); }
867+
void reset(EC_GROUP* group = nullptr);
868+
EC_GROUP* release();
869+
870+
static ECGroupPointer NewByCurveName(int nid);
871+
872+
private:
873+
DeleteFnPtr<EC_GROUP, EC_GROUP_free> group_;
874+
};
875+
855876
#ifndef OPENSSL_NO_ENGINE
856877
class EnginePointer final {
857878
public:

src/crypto/crypto_ec.cc

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -399,15 +399,11 @@ void ECDH::ConvertKey(const FunctionCallbackInfo<Value>& args) {
399399
if (nid == NID_undef)
400400
return THROW_ERR_CRYPTO_INVALID_CURVE(env);
401401

402-
ECGroupPointer group(
403-
EC_GROUP_new_by_curve_name(nid));
404-
if (group == nullptr)
402+
auto group = ECGroupPointer::NewByCurveName(nid);
403+
if (!group)
405404
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to get EC_GROUP");
406405

407-
ECPointPointer pub(
408-
ECDH::BufferToPoint(env,
409-
group.get(),
410-
args[0]));
406+
ECPointPointer pub(ECDH::BufferToPoint(env, group, args[0]));
411407

412408
if (pub == nullptr) {
413409
return THROW_ERR_CRYPTO_OPERATION_FAILED(env,
@@ -420,7 +416,7 @@ void ECDH::ConvertKey(const FunctionCallbackInfo<Value>& args) {
420416

421417
const char* error;
422418
Local<Object> buf;
423-
if (!ECPointToBuffer(env, group.get(), pub.get(), form, &error).ToLocal(&buf))
419+
if (!ECPointToBuffer(env, group, pub.get(), form, &error).ToLocal(&buf))
424420
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, error);
425421
args.GetReturnValue().Set(buf);
426422
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy