util: Redefine method for random number generation
The previous definitions random::generateWord* now generate
(cryptographically) "non-secure" random numbers from uniform
distribution using Boost.Random routines. When (cryptographically)
secure random numbers are necessary, random::generateSecureWord*
routines should be used instead.
Change-Id: I52561119f7e558b4cfe0d250ca8a89e550d21b5f
Refs: #1760
diff --git a/src/util/random.cpp b/src/util/random.cpp
index 3310d1b..f321bbd 100644
--- a/src/util/random.cpp
+++ b/src/util/random.cpp
@@ -23,30 +23,66 @@
#include "random.hpp"
+#include <boost/nondet_random.hpp>
+#include <boost/random/mersenne_twister.hpp>
+#include <boost/random/uniform_int_distribution.hpp>
+
#include "../security/cryptopp.hpp"
namespace ndn {
namespace random {
-uint32_t
-generateWord32()
+// CryptoPP-based (secure) random generators
+
+static CryptoPP::AutoSeededRandomPool&
+getSecureRandomGenerator()
{
static CryptoPP::AutoSeededRandomPool rng;
- return rng.GenerateWord32();
+ return rng;
+}
+
+uint32_t
+generateSecureWord32()
+{
+ return getSecureRandomGenerator().GenerateWord32();
+}
+
+uint64_t
+generateSecureWord64()
+{
+ uint64_t random;
+ getSecureRandomGenerator()
+ .GenerateBlock(reinterpret_cast<unsigned char*>(&random), sizeof(uint64_t));
+
+ return random;
+}
+
+// Boost.Random-based (simple) random generators
+
+static boost::random::mt19937&
+getRandomGenerator()
+{
+ static boost::random_device randomSeedGenerator;
+ static boost::random::mt19937 gen(randomSeedGenerator);
+
+ return gen;
+}
+
+uint32_t
+generateWord32()
+{
+ static boost::random::uniform_int_distribution<uint32_t> distribution;
+ return distribution(getRandomGenerator());
}
uint64_t
generateWord64()
{
- static CryptoPP::AutoSeededRandomPool rng;
-
- uint64_t random;
-
- rng.GenerateBlock(reinterpret_cast<unsigned char*>(&random), 8);
-
- return random;
+ static boost::random::uniform_int_distribution<uint64_t> distribution;
+ return distribution(getRandomGenerator());
}
+
} // namespace random
} // namespace ndn
diff --git a/src/util/random.hpp b/src/util/random.hpp
index afa1758..6dc1b52 100644
--- a/src/util/random.hpp
+++ b/src/util/random.hpp
@@ -27,9 +27,43 @@
namespace ndn {
namespace random {
+/**
+ * @brief Generate a cryptographically secure random integer from the range [0, 2^32)
+ *
+ * This method uses CryptoPP routines
+ */
+uint32_t
+generateSecureWord32();
+
+/**
+ * @brief Generate a cryptographically secure random integer from the range [0, 2^64)
+ *
+ * This method uses CryptoPP routines
+ */
+uint64_t
+generateSecureWord64();
+
+/**
+ * @brief Generate a cryptographically non-secure random integer from the range [0, 2^32)
+ *
+ * This method uses Boost.Random routines
+ *
+ * This version is faster than generateSecureWord32, but it should not be used when
+ * cryptographically secure random integers are needed (e.g., when creating signing or
+ * encryption keys)
+ */
uint32_t
generateWord32();
+/**
+ * @brief Generate a cryptographically non-secure random integer from range [0, 2^64)
+ *
+ * This method uses Boost.Random routines
+ *
+ * This version is faster than generateSecureWord64, but it should not be used when
+ * cryptographically secure random integers are needed (e.g., when creating signing or
+ * encryption keys)
+ */
uint64_t
generateWord64();