aboutsummaryrefslogtreecommitdiff
path: root/src/crypto
diff options
context:
space:
mode:
Diffstat (limited to 'src/crypto')
-rw-r--r--src/crypto/crypto.cpp15
-rw-r--r--src/crypto/crypto.h1
-rw-r--r--src/crypto/random.c15
-rw-r--r--src/crypto/random.h1
-rw-r--r--src/crypto/slow-hash.c6
5 files changed, 33 insertions, 5 deletions
diff --git a/src/crypto/crypto.cpp b/src/crypto/crypto.cpp
index d4b2a22bc..0ec992de9 100644
--- a/src/crypto/crypto.cpp
+++ b/src/crypto/crypto.cpp
@@ -88,13 +88,24 @@ namespace crypto {
return &reinterpret_cast<const unsigned char &>(scalar);
}
- void generate_random_bytes_thread_safe(size_t N, uint8_t *bytes)
+ boost::mutex &get_random_lock()
{
static boost::mutex random_lock;
- boost::lock_guard<boost::mutex> lock(random_lock);
+ return random_lock;
+ }
+
+ void generate_random_bytes_thread_safe(size_t N, uint8_t *bytes)
+ {
+ boost::lock_guard<boost::mutex> lock(get_random_lock());
generate_random_bytes_not_thread_safe(N, bytes);
}
+ void add_extra_entropy_thread_safe(const void *ptr, size_t bytes)
+ {
+ boost::lock_guard<boost::mutex> lock(get_random_lock());
+ add_extra_entropy_not_thread_safe(ptr, bytes);
+ }
+
static inline bool less32(const unsigned char *k0, const unsigned char *k1)
{
for (int n = 31; n >= 0; --n)
diff --git a/src/crypto/crypto.h b/src/crypto/crypto.h
index bac456f60..8ce321f71 100644
--- a/src/crypto/crypto.h
+++ b/src/crypto/crypto.h
@@ -147,6 +147,7 @@ namespace crypto {
};
void generate_random_bytes_thread_safe(size_t N, uint8_t *bytes);
+ void add_extra_entropy_thread_safe(const void *ptr, size_t bytes);
/* Generate N random bytes
*/
diff --git a/src/crypto/random.c b/src/crypto/random.c
index 74b202661..766b5f558 100644
--- a/src/crypto/random.c
+++ b/src/crypto/random.c
@@ -146,3 +146,18 @@ void generate_random_bytes_not_thread_safe(size_t n, void *result) {
}
}
}
+
+void add_extra_entropy_not_thread_safe(const void *ptr, size_t bytes)
+{
+ size_t i;
+
+ while (bytes > 0)
+ {
+ hash_permutation(&state);
+ const size_t round_bytes = bytes > HASH_DATA_AREA ? HASH_DATA_AREA : bytes;
+ for (i = 0; i < round_bytes; ++i)
+ state.b[i] ^= ((const uint8_t*)ptr)[i];
+ bytes -= round_bytes;
+ ptr = cpadd(ptr, round_bytes);
+ }
+}
diff --git a/src/crypto/random.h b/src/crypto/random.h
index ccb9f4853..21a66d776 100644
--- a/src/crypto/random.h
+++ b/src/crypto/random.h
@@ -33,3 +33,4 @@
#include <stddef.h>
void generate_random_bytes_not_thread_safe(size_t n, void *result);
+void add_extra_entropy_not_thread_safe(const void *ptr, size_t bytes);
diff --git a/src/crypto/slow-hash.c b/src/crypto/slow-hash.c
index 1fa819b57..647471513 100644
--- a/src/crypto/slow-hash.c
+++ b/src/crypto/slow-hash.c
@@ -136,8 +136,8 @@ static inline int use_v4_jit(void)
{ \
U64(b)[2] = state.hs.w[8] ^ state.hs.w[10]; \
U64(b)[3] = state.hs.w[9] ^ state.hs.w[11]; \
- division_result = state.hs.w[12]; \
- sqrt_result = state.hs.w[13]; \
+ division_result = SWAP64LE(state.hs.w[12]); \
+ sqrt_result = SWAP64LE(state.hs.w[13]); \
} while (0)
#define VARIANT2_PORTABLE_INIT() \
@@ -210,7 +210,7 @@ static inline int use_v4_jit(void)
uint64_t b0[2]; \
memcpy_swap64le(b0, b, 2); \
chunk2[0] = SWAP64LE(chunk1_old[0] + b0[0]); \
- chunk2[1] = SWAP64LE(SWAP64LE(chunk1_old[1]) + b0[1]); \
+ chunk2[1] = SWAP64LE(chunk1_old[1] + b0[1]); \
if (variant >= 4) \
{ \
uint64_t out_copy[2]; \