Skip to content

Commit 100ac4e

Browse files
theunilaanwj
authored andcommitted
consensus: cache the openssl EC_GROUP to avoid a race condition for each CECKey instantiation
Github-Pull: #6571 Rebased-From: 1d1073c
1 parent 93b606a commit 100ac4e

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

src/ecwrapper.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,29 @@
1313

1414
namespace {
1515

16+
class ecgroup_order
17+
{
18+
public:
19+
static const EC_GROUP* get()
20+
{
21+
static const ecgroup_order wrapper;
22+
return wrapper.pgroup;
23+
}
24+
25+
private:
26+
ecgroup_order()
27+
: pgroup(EC_GROUP_new_by_curve_name(NID_secp256k1))
28+
{
29+
}
30+
31+
~ecgroup_order()
32+
{
33+
EC_GROUP_free(pgroup);
34+
}
35+
36+
EC_GROUP* pgroup;
37+
};
38+
1639
/**
1740
* Perform ECDSA key recovery (see SEC1 4.1.6) for curves over (mod p)-fields
1841
* recid selects which key is recovered
@@ -92,8 +115,10 @@ int ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, ECDSA_SIG *ecsig, const unsigned ch
92115
} // anon namespace
93116

94117
CECKey::CECKey() {
95-
pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
118+
pkey = EC_KEY_new();
96119
assert(pkey != NULL);
120+
int result = EC_KEY_set_group(pkey, ecgroup_order::get());
121+
assert(result);
97122
}
98123

99124
CECKey::~CECKey() {
@@ -185,11 +210,9 @@ bool CECKey::TweakPublic(const unsigned char vchTweak[32]) {
185210

186211
bool CECKey::SanityCheck()
187212
{
188-
EC_KEY *pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
189-
if(pkey == NULL)
213+
const EC_GROUP *pgroup = ecgroup_order::get();
214+
if(pgroup == NULL)
190215
return false;
191-
EC_KEY_free(pkey);
192-
193216
// TODO Is there more EC functionality that could be missing?
194217
return true;
195218
}

0 commit comments

Comments
 (0)