diff options
author | luigi1111 <luigi1111w@gmail.com> | 2018-05-31 14:41:17 -0500 |
---|---|---|
committer | luigi1111 <luigi1111w@gmail.com> | 2018-05-31 14:41:17 -0500 |
commit | b6eb7484d1f8b7055a20f116b07a0787019e2be7 (patch) | |
tree | 884cec37eb1760515b89602afe0188c05e697b46 /src | |
parent | Merge pull request #3583 (diff) | |
parent | unit_tests: add ringdb unit tests (diff) | |
download | monero-b6eb7484d1f8b7055a20f116b07a0787019e2be7.tar.xz |
Merge pull request #3592
90a16b1 crypto: fix initialization order issue with random mutex (moneromooo-monero)
6a61f52 unit_tests: add ringdb unit tests (moneromooo-monero)
Diffstat (limited to 'src')
-rw-r--r-- | src/crypto/crypto.cpp | 15 | ||||
-rw-r--r-- | src/crypto/crypto.h | 10 | ||||
-rw-r--r-- | src/wallet/ringdb.cpp | 18 | ||||
-rw-r--r-- | src/wallet/ringdb.h | 1 |
4 files changed, 30 insertions, 14 deletions
diff --git a/src/crypto/crypto.cpp b/src/crypto/crypto.cpp index ba0149240..f4ef751d3 100644 --- a/src/crypto/crypto.cpp +++ b/src/crypto/crypto.cpp @@ -70,8 +70,6 @@ namespace crypto { #include "random.h" } - boost::mutex random_lock; - static inline unsigned char *operator &(ec_point &point) { return &reinterpret_cast<unsigned char &>(point); } @@ -88,6 +86,13 @@ namespace crypto { return &reinterpret_cast<const unsigned char &>(scalar); } + void generate_random_bytes_thread_safe(size_t N, uint8_t *bytes) + { + static boost::mutex random_lock; + boost::lock_guard<boost::mutex> lock(random_lock); + generate_random_bytes_not_thread_safe(N, bytes); + } + /* generate a random 32-byte (256-bit) integer and copy it to res */ static inline void random_scalar_not_thread_safe(ec_scalar &res) { unsigned char tmp[64]; @@ -96,8 +101,10 @@ namespace crypto { memcpy(&res, tmp, 32); } static inline void random_scalar(ec_scalar &res) { - boost::lock_guard<boost::mutex> lock(random_lock); - random_scalar_not_thread_safe(res); + unsigned char tmp[64]; + generate_random_bytes_thread_safe(64, tmp); + sc_reduce(tmp); + memcpy(&res, tmp, 32); } void hash_to_scalar(const void *data, size_t length, ec_scalar &res) { diff --git a/src/crypto/crypto.h b/src/crypto/crypto.h index 81ebfb9e2..9ea0f2ec0 100644 --- a/src/crypto/crypto.h +++ b/src/crypto/crypto.h @@ -53,8 +53,6 @@ namespace crypto { #include "random.h" } - extern boost::mutex random_lock; - #pragma pack(push, 1) POD_CLASS ec_point { char data[32]; @@ -149,11 +147,12 @@ namespace crypto { const public_key *const *, std::size_t, const signature *); }; + void generate_random_bytes_thread_safe(size_t N, uint8_t *bytes); + /* Generate N random bytes */ inline void rand(size_t N, uint8_t *bytes) { - boost::lock_guard<boost::mutex> lock(random_lock); - generate_random_bytes_not_thread_safe(N, bytes); + generate_random_bytes_thread_safe(N, bytes); } /* Generate a value filled with random bytes. @@ -161,8 +160,7 @@ namespace crypto { template<typename T> typename std::enable_if<std::is_pod<T>::value, T>::type rand() { typename std::remove_cv<T>::type res; - boost::lock_guard<boost::mutex> lock(random_lock); - generate_random_bytes_not_thread_safe(sizeof(T), &res); + generate_random_bytes_thread_safe(sizeof(T), (uint8_t*)&res); return res; } diff --git a/src/wallet/ringdb.cpp b/src/wallet/ringdb.cpp index 44992520f..3f2634c8b 100644 --- a/src/wallet/ringdb.cpp +++ b/src/wallet/ringdb.cpp @@ -190,7 +190,8 @@ namespace tools { ringdb::ringdb(std::string filename, const std::string &genesis): - filename(filename) + filename(filename), + env(NULL) { MDB_txn *txn; bool tx_active = false; @@ -227,9 +228,18 @@ ringdb::ringdb(std::string filename, const std::string &genesis): ringdb::~ringdb() { - mdb_dbi_close(env, dbi_rings); - mdb_dbi_close(env, dbi_blackballs); - mdb_env_close(env); + close(); +} + +void ringdb::close() +{ + if (env) + { + mdb_dbi_close(env, dbi_rings); + mdb_dbi_close(env, dbi_blackballs); + mdb_env_close(env); + env = NULL; + } } bool ringdb::add_rings(const crypto::chacha_key &chacha_key, const cryptonote::transaction_prefix &tx) diff --git a/src/wallet/ringdb.h b/src/wallet/ringdb.h index 2bd1ac149..6b4bce124 100644 --- a/src/wallet/ringdb.h +++ b/src/wallet/ringdb.h @@ -41,6 +41,7 @@ namespace tools { public: ringdb(std::string filename, const std::string &genesis); + void close(); ~ringdb(); bool add_rings(const crypto::chacha_key &chacha_key, const cryptonote::transaction_prefix &tx); |