aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2015-08-20 16:24:36 +0200
committerWladimir J. van der Laan <laanwj@gmail.com>2015-08-20 16:33:50 +0200
commit5e6e0898a1115c6badd3f4ddf2f42e571471b54f (patch)
treeaada7a631ce0752b342b3be988dbcc59e53cd7ba /src
parentef30389e2a4a83c2845a2cbdf3a7c2f062404077 (diff)
parent1d1073c2d3231a9de4adbc2bc46846fc20b34f9e (diff)
downloadbitcoin-5e6e0898a1115c6badd3f4ddf2f42e571471b54f.tar.xz
Merge pull request #6571
1d1073c consensus: cache the openssl EC_GROUP to avoid a race condition for each CECKey instantiation (Cory Fields)
Diffstat (limited to 'src')
-rw-r--r--src/ecwrapper.cpp33
1 files changed, 28 insertions, 5 deletions
diff --git a/src/ecwrapper.cpp b/src/ecwrapper.cpp
index 5e3aec25ba..f94bc954fd 100644
--- a/src/ecwrapper.cpp
+++ b/src/ecwrapper.cpp
@@ -13,6 +13,29 @@
namespace {
+class ecgroup_order
+{
+public:
+ static const EC_GROUP* get()
+ {
+ static const ecgroup_order wrapper;
+ return wrapper.pgroup;
+ }
+
+private:
+ ecgroup_order()
+ : pgroup(EC_GROUP_new_by_curve_name(NID_secp256k1))
+ {
+ }
+
+ ~ecgroup_order()
+ {
+ EC_GROUP_free(pgroup);
+ }
+
+ EC_GROUP* pgroup;
+};
+
/**
* Perform ECDSA key recovery (see SEC1 4.1.6) for curves over (mod p)-fields
* recid selects which key is recovered
@@ -92,8 +115,10 @@ err:
} // anon namespace
CECKey::CECKey() {
- pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
+ pkey = EC_KEY_new();
assert(pkey != NULL);
+ int result = EC_KEY_set_group(pkey, ecgroup_order::get());
+ assert(result);
}
CECKey::~CECKey() {
@@ -185,11 +210,9 @@ bool CECKey::TweakPublic(const unsigned char vchTweak[32]) {
bool CECKey::SanityCheck()
{
- EC_KEY *pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
- if(pkey == NULL)
+ const EC_GROUP *pgroup = ecgroup_order::get();
+ if(pgroup == NULL)
return false;
- EC_KEY_free(pkey);
-
// TODO Is there more EC functionality that could be missing?
return true;
}