mirror of
https://codeberg.org/anoncontributorxmr/monero.git
synced 2024-11-24 00:12:25 +00:00
crypto: make clear generate_random_bytes is not thread safe
And add a thread safe version to encourage proper use
This commit is contained in:
parent
8b0d22a2aa
commit
d539be3359
4 changed files with 11 additions and 4 deletions
|
@ -83,7 +83,7 @@ namespace crypto {
|
||||||
/* 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(ec_scalar &res) {
|
static inline void random_scalar(ec_scalar &res) {
|
||||||
unsigned char tmp[64];
|
unsigned char tmp[64];
|
||||||
generate_random_bytes(64, tmp);
|
generate_random_bytes_not_thread_safe(64, tmp);
|
||||||
sc_reduce(tmp);
|
sc_reduce(tmp);
|
||||||
memcpy(&res, tmp, 32);
|
memcpy(&res, tmp, 32);
|
||||||
}
|
}
|
||||||
|
|
|
@ -117,13 +117,20 @@ namespace crypto {
|
||||||
const public_key *const *, std::size_t, const signature *);
|
const public_key *const *, std::size_t, const signature *);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* 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 a value filled with random bytes.
|
/* Generate a value filled with random bytes.
|
||||||
*/
|
*/
|
||||||
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);
|
boost::lock_guard<boost::mutex> lock(random_lock);
|
||||||
generate_random_bytes(sizeof(T), &res);
|
generate_random_bytes_not_thread_safe(sizeof(T), &res);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -113,7 +113,7 @@ INITIALIZER(init_random) {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void generate_random_bytes(size_t n, void *result) {
|
void generate_random_bytes_not_thread_safe(size_t n, void *result) {
|
||||||
#if !defined(NDEBUG)
|
#if !defined(NDEBUG)
|
||||||
assert(curstate == 1);
|
assert(curstate == 1);
|
||||||
curstate = 2;
|
curstate = 2;
|
||||||
|
|
|
@ -32,4 +32,4 @@
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
void generate_random_bytes(size_t n, void *result);
|
void generate_random_bytes_not_thread_safe(size_t n, void *result);
|
||||||
|
|
Loading…
Reference in a new issue