Merge commit 'f68512e9e41fa9447508dedefe6aec94c94cf6df' into MergeUpstream3

This commit is contained in:
Doyle 2020-05-18 16:16:29 +10:00
commit 90d53cac90
7 changed files with 45 additions and 4 deletions

View file

@ -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)

View file

@ -134,6 +134,7 @@ namespace crypto {
sizeof(signature) == 64, "Invalid structure size");
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
*/

View file

@ -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);
}
}

View file

@ -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);

View file

@ -321,7 +321,9 @@ namespace hw {
bool device_ledger::reset() {
reset_buffer();
int offset = set_command_header_noopt(INS_RESET);
memmove(this->buffer_send+offset, LOKI_VERSION_STR, strlen(LOKI_VERSION_STR));
const size_t verlen = strlen(LOKI_VERSION_STR);
ASSERT_X(offset + verlen <= BUFFER_SEND_SIZE, "LOKI_VERSION_STR is too long")
memmove(this->buffer_send+offset, LOKI_VERSION_STR, verlen);
offset += strlen(LOKI_VERSION_STR);
this->buffer_send[4] = offset-5;
this->length_send = offset;

View file

@ -76,7 +76,7 @@ namespace hw {
rct::key AKout;
ABPkeys(const rct::key& A, const rct::key& B, const bool is_subaddr, bool is_subaddress, bool is_change_address, size_t index, const rct::key& P,const rct::key& AK);
ABPkeys(const ABPkeys& keys) ;
ABPkeys() {index=0;is_subaddress=false;is_subaddress=false;is_change_address=false;}
ABPkeys() {index=0;is_subaddress=false;is_change_address=false;additional_key=false;}
ABPkeys &operator=(const ABPkeys &keys);
};

View file

@ -267,6 +267,7 @@ struct options {
const command_line::arg_descriptor<std::string> hw_device_derivation_path = {"hw-device-deriv-path", tools::wallet2::tr("HW device wallet derivation path (e.g., SLIP-10)"), ""};
const command_line::arg_descriptor<std::string> tx_notify = { "tx-notify" , "Run a program for each new incoming transaction, '%s' will be replaced by the transaction hash" , "" };
const command_line::arg_descriptor<bool> offline = {"offline", tools::wallet2::tr("Do not connect to a daemon"), false};
const command_line::arg_descriptor<std::string> extra_entropy = {"extra-entropy", tools::wallet2::tr("File containing extra entropy to initialize the PRNG (any data, aim for 256 bits of entropy to be useful, wihch typically means more than 256 bits of data)")};
};
void do_prepare_file_names(const std::string& file_path, std::string& keys_file, std::string& wallet_file, std::string &mms_file)
@ -463,6 +464,15 @@ std::unique_ptr<tools::wallet2> make_basic(const boost::program_options::variabl
if (command_line::get_arg(vm, opts.offline))
wallet->set_offline();
const std::string extra_entropy = command_line::get_arg(vm, opts.extra_entropy);
if (!extra_entropy.empty())
{
std::string data;
THROW_WALLET_EXCEPTION_IF(!epee::file_io_utils::load_file_to_string(extra_entropy, data),
tools::error::wallet_internal_error, "Failed to load extra entropy from " + extra_entropy);
add_extra_entropy_thread_safe(data.data(), data.size());
}
try
{
if (!command_line::is_arg_defaulted(vm, opts.tx_notify))
@ -1160,6 +1170,7 @@ void wallet2::init_options(boost::program_options::options_description& desc_par
command_line::add_arg(desc_params, opts.tx_notify);
command_line::add_arg(desc_params, opts.offline);
command_line::add_arg(desc_params, opts.disable_rpc_long_poll);
command_line::add_arg(desc_params, opts.extra_entropy);
}
std::pair<std::unique_ptr<wallet2>, tools::password_container> wallet2::make_from_json(const boost::program_options::variables_map& vm, bool unattended, const std::string& json_file, const std::function<boost::optional<tools::password_container>(const char *, bool)> &password_prompter)