mirror of
https://codeberg.org/anoncontributorxmr/monero.git
synced 2024-11-29 18:53:14 +00:00
crypto: fix initialization order issue with random mutex
This commit is contained in:
parent
8361d60aef
commit
90a16b119f
2 changed files with 15 additions and 10 deletions
|
@ -70,8 +70,6 @@ namespace crypto {
|
||||||
#include "random.h"
|
#include "random.h"
|
||||||
}
|
}
|
||||||
|
|
||||||
boost::mutex random_lock;
|
|
||||||
|
|
||||||
static inline unsigned char *operator &(ec_point &point) {
|
static inline unsigned char *operator &(ec_point &point) {
|
||||||
return &reinterpret_cast<unsigned char &>(point);
|
return &reinterpret_cast<unsigned char &>(point);
|
||||||
}
|
}
|
||||||
|
@ -88,6 +86,13 @@ namespace crypto {
|
||||||
return &reinterpret_cast<const unsigned char &>(scalar);
|
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 */
|
/* generate a random 32-byte (256-bit) integer and copy it to res */
|
||||||
static inline void random_scalar_not_thread_safe(ec_scalar &res) {
|
static inline void random_scalar_not_thread_safe(ec_scalar &res) {
|
||||||
unsigned char tmp[64];
|
unsigned char tmp[64];
|
||||||
|
@ -96,8 +101,10 @@ namespace crypto {
|
||||||
memcpy(&res, tmp, 32);
|
memcpy(&res, tmp, 32);
|
||||||
}
|
}
|
||||||
static inline void random_scalar(ec_scalar &res) {
|
static inline void random_scalar(ec_scalar &res) {
|
||||||
boost::lock_guard<boost::mutex> lock(random_lock);
|
unsigned char tmp[64];
|
||||||
random_scalar_not_thread_safe(res);
|
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) {
|
void hash_to_scalar(const void *data, size_t length, ec_scalar &res) {
|
||||||
|
|
|
@ -53,8 +53,6 @@ namespace crypto {
|
||||||
#include "random.h"
|
#include "random.h"
|
||||||
}
|
}
|
||||||
|
|
||||||
extern boost::mutex random_lock;
|
|
||||||
|
|
||||||
#pragma pack(push, 1)
|
#pragma pack(push, 1)
|
||||||
POD_CLASS ec_point {
|
POD_CLASS ec_point {
|
||||||
char data[32];
|
char data[32];
|
||||||
|
@ -149,11 +147,12 @@ namespace crypto {
|
||||||
const public_key *const *, std::size_t, const signature *);
|
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
|
/* Generate N random bytes
|
||||||
*/
|
*/
|
||||||
inline void rand(size_t N, uint8_t *bytes) {
|
inline void rand(size_t N, uint8_t *bytes) {
|
||||||
boost::lock_guard<boost::mutex> lock(random_lock);
|
generate_random_bytes_thread_safe(N, bytes);
|
||||||
generate_random_bytes_not_thread_safe(N, bytes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Generate a value filled with random bytes.
|
/* Generate a value filled with random bytes.
|
||||||
|
@ -161,8 +160,7 @@ namespace crypto {
|
||||||
template<typename T>
|
template<typename T>
|
||||||
typename std::enable_if<std::is_pod<T>::value, T>::type rand() {
|
typename std::enable_if<std::is_pod<T>::value, T>::type rand() {
|
||||||
typename std::remove_cv<T>::type res;
|
typename std::remove_cv<T>::type res;
|
||||||
boost::lock_guard<boost::mutex> lock(random_lock);
|
generate_random_bytes_thread_safe(sizeof(T), (uint8_t*)&res);
|
||||||
generate_random_bytes_not_thread_safe(sizeof(T), &res);
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue