Improve pool seeding during startup:

* When starting up, we no longer rely just on the standard
  system RNG to generate entropy: we attempt to squeeze some
  from the execution state, and to recover any entropy that
  we had previously stored.

* When shutting down, if sufficient entropy has been accumulated
  attempt to store it for future use.
This commit is contained in:
Nik Bougalis
2015-02-17 19:14:01 -08:00
committed by Tom Ritchford
parent 382a16ff07
commit 84e618b3f2
5 changed files with 79 additions and 4 deletions

View File

@@ -41,6 +41,7 @@
#include <boost/program_options.hpp>
#include <cstdlib>
#include <thread>
#include <utility>
#if defined(BEAST_LINUX) || defined(BEAST_MAC) || defined(BEAST_BSD)
#include <sys/resource.h>
@@ -67,6 +68,13 @@ void setupServer ()
getApp().setup ();
}
boost::filesystem::path
getEntropyFile()
{
return boost::filesystem::path (
getConfig().legacy("database_path")) / "random.seed";
}
void startServer ()
{
//
@@ -93,7 +101,11 @@ void startServer ()
}
}
getApp().run (); // Blocks till we get a stop RPC.
// Block until we get a stop RPC.
getApp().run ();
// Try to write out some entropy to use the next time we start.
stir_entropy (getEntropyFile ().string ());
}
void printHelp (const po::options_description& desc)
@@ -258,8 +270,29 @@ int run (int argc, char** argv)
po::positional_options_description p;
p.add ("parameters", -1);
// Seed the RNG early
add_entropy ();
{
// We want to seed the RNG early. We acquire a small amount of
// questionable quality entropy from the current time and our
// environment block which will get stirred into the RNG pool
// along with high-quality entropy from the system.
struct entropy_t
{
std::uint64_t timestamp;
std::size_t tid;
std::uintptr_t ptr[4];
};
auto entropy = std::make_unique<entropy_t> ();
entropy->timestamp = beast::Time::currentTimeMillis ();
entropy->tid = std::hash <std::thread::id>() (std::this_thread::get_id ());
entropy->ptr[0] = reinterpret_cast<std::uintptr_t>(entropy.get ());
entropy->ptr[1] = reinterpret_cast<std::uintptr_t>(&argc);
entropy->ptr[2] = reinterpret_cast<std::uintptr_t>(argv);
entropy->ptr[3] = reinterpret_cast<std::uintptr_t>(argv[0]);
add_entropy (entropy.get (), sizeof (entropy_t));
}
if (!iResult)
{
@@ -346,6 +379,9 @@ int run (int argc, char** argv)
getConfig ().RUN_STANDALONE = true;
getConfig ().LEDGER_HISTORY = 0;
}
// Use any previously available entropy to stir the pool
stir_entropy (getEntropyFile ().string ());
}
if (vm.count ("start")) getConfig ().START_UP = Config::FRESH;