[random] move `Random::Manager` in `ranadom.hpp` (#7603)
This commit moves and renames the `Random::Manager` class to
the `random.hpp` header file (deleting the `random_manager.hpp`).
It also moves the implementation of some the `Random` functions
e.g. `GetUint8InRange()` or `FillBuffer()` (which are more complex)
to the `cpp` file (instead of defined as `inline`)
diff --git a/Android.mk b/Android.mk
index ce27a3b..b144b54 100644
--- a/Android.mk
+++ b/Android.mk
@@ -238,7 +238,7 @@
src/core/common/log.cpp \
src/core/common/message.cpp \
src/core/common/notifier.cpp \
- src/core/common/random_manager.cpp \
+ src/core/common/random.cpp \
src/core/common/settings.cpp \
src/core/common/string.cpp \
src/core/common/tasklet.cpp \
diff --git a/src/core/BUILD.gn b/src/core/BUILD.gn
index cb8d077..e5527a6 100644
--- a/src/core/BUILD.gn
+++ b/src/core/BUILD.gn
@@ -423,9 +423,8 @@
"common/owning_list.hpp",
"common/pool.hpp",
"common/ptr_wrapper.hpp",
+ "common/random.cpp",
"common/random.hpp",
- "common/random_manager.cpp",
- "common/random_manager.hpp",
"common/retain_ptr.hpp",
"common/serial_number.hpp",
"common/settings.cpp",
@@ -707,7 +706,7 @@
"common/error.hpp",
"common/instance.cpp",
"common/log.cpp",
- "common/random_manager.cpp",
+ "common/random.cpp",
"common/string.cpp",
"common/tasklet.cpp",
"common/timer.cpp",
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index f6e9f1b..d1b1d71 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -104,7 +104,7 @@
common/log.cpp
common/message.cpp
common/notifier.cpp
- common/random_manager.cpp
+ common/random.cpp
common/settings.cpp
common/string.cpp
common/tasklet.cpp
@@ -248,7 +248,7 @@
common/error.cpp
common/instance.cpp
common/log.cpp
- common/random_manager.cpp
+ common/random.cpp
common/string.cpp
common/tasklet.cpp
common/timer.cpp
diff --git a/src/core/Makefile.am b/src/core/Makefile.am
index a607faa..2d1e776 100644
--- a/src/core/Makefile.am
+++ b/src/core/Makefile.am
@@ -194,7 +194,7 @@
common/log.cpp \
common/message.cpp \
common/notifier.cpp \
- common/random_manager.cpp \
+ common/random.cpp \
common/settings.cpp \
common/string.cpp \
common/tasklet.cpp \
@@ -338,7 +338,7 @@
common/error.cpp \
common/instance.cpp \
common/log.cpp \
- common/random_manager.cpp \
+ common/random.cpp \
common/string.cpp \
common/tasklet.cpp \
common/timer.cpp \
@@ -456,7 +456,6 @@
common/pool.hpp \
common/ptr_wrapper.hpp \
common/random.hpp \
- common/random_manager.hpp \
common/retain_ptr.hpp \
common/serial_number.hpp \
common/settings.hpp \
diff --git a/src/core/common/instance.hpp b/src/core/common/instance.hpp
index a184a09..d49b6c0 100644
--- a/src/core/common/instance.hpp
+++ b/src/core/common/instance.hpp
@@ -51,7 +51,7 @@
#include "common/log.hpp"
#include "common/message.hpp"
#include "common/non_copyable.hpp"
-#include "common/random_manager.hpp"
+#include "common/random.hpp"
#include "common/tasklet.hpp"
#include "common/time_ticker.hpp"
#include "common/timer.hpp"
@@ -331,7 +331,7 @@
#endif
#if OPENTHREAD_MTD || OPENTHREAD_FTD
- // RandomManager is initialized before other objects. Note that it
+ // Random::Manager is initialized before other objects. Note that it
// requires MbedTls which itself may use Heap.
#if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
static Utils::Heap sHeap;
@@ -339,7 +339,7 @@
Crypto::MbedTls mMbedTls;
#endif // OPENTHREAD_MTD || OPENTHREAD_FTD
- RandomManager mRandomManager;
+ Random::Manager mRandomManager;
// Radio is initialized before other member variables
// (particularly, SubMac and Mac) to allow them to use its methods
diff --git a/src/core/common/random_manager.cpp b/src/core/common/random.cpp
similarity index 72%
rename from src/core/common/random_manager.cpp
rename to src/core/common/random.cpp
index e059bde..482d68d 100644
--- a/src/core/common/random_manager.cpp
+++ b/src/core/common/random.cpp
@@ -31,21 +31,20 @@
* This file provides an implementation of OpenThread random number generation manager class.
*/
-#include "random_manager.hpp"
+#include "random.hpp"
#include <openthread/platform/entropy.h>
#include "common/code_utils.hpp"
#include "common/debug.hpp"
-#include "common/random.hpp"
-#include "crypto/mbedtls.hpp"
namespace ot {
+namespace Random {
-uint16_t RandomManager::sInitCount = 0;
-RandomManager::NonCryptoPrng RandomManager::sPrng;
+uint16_t Manager::sInitCount = 0;
+Manager::NonCryptoPrng Manager::sPrng;
-RandomManager::RandomManager(void)
+Manager::Manager(void)
{
uint32_t seed;
@@ -66,7 +65,7 @@
sInitCount++;
}
-RandomManager::~RandomManager(void)
+Manager::~Manager(void)
{
OT_ASSERT(sInitCount > 0);
@@ -81,7 +80,7 @@
return;
}
-uint32_t RandomManager::NonCryptoGetUint32(void)
+uint32_t Manager::NonCryptoGetUint32(void)
{
OT_ASSERT(sInitCount > 0);
@@ -91,7 +90,7 @@
//-------------------------------------------------------------------
// NonCryptoPrng
-void RandomManager::NonCryptoPrng::Init(uint32_t aSeed)
+void Manager::NonCryptoPrng::Init(uint32_t aSeed)
{
// The PRNG has a cycle of length 1 for the below two initial
// seeds. For all other seed values the cycle is ~2^31 long.
@@ -104,7 +103,7 @@
mState = aSeed;
}
-uint32_t RandomManager::NonCryptoPrng::GetNext(void)
+uint32_t Manager::NonCryptoPrng::GetNext(void)
{
uint32_t mlcg, p, q;
uint64_t tmpstate;
@@ -126,4 +125,44 @@
return mlcg;
}
+//-------------------------------------------------------------------
+
+namespace NonCrypto {
+
+uint8_t GetUint8InRange(uint8_t aMin, uint8_t aMax)
+{
+ OT_ASSERT(aMax > aMin);
+
+ return (aMin + (GetUint8() % (aMax - aMin)));
+}
+
+uint16_t GetUint16InRange(uint16_t aMin, uint16_t aMax)
+{
+ OT_ASSERT(aMax > aMin);
+ return (aMin + (GetUint16() % (aMax - aMin)));
+}
+
+uint32_t GetUint32InRange(uint32_t aMin, uint32_t aMax)
+{
+ OT_ASSERT(aMax > aMin);
+ return (aMin + (GetUint32() % (aMax - aMin)));
+}
+
+void FillBuffer(uint8_t *aBuffer, uint16_t aSize)
+{
+ while (aSize-- != 0)
+ {
+ *aBuffer++ = GetUint8();
+ }
+}
+
+uint32_t AddJitter(uint32_t aValue, uint16_t aJitter)
+{
+ aJitter = (aJitter <= aValue) ? aJitter : static_cast<uint16_t>(aValue);
+
+ return aValue + GetUint32InRange(0, 2 * aJitter + 1) - aJitter;
+}
+
+} // namespace NonCrypto
+} // namespace Random
} // namespace ot
diff --git a/src/core/common/random.hpp b/src/core/common/random.hpp
index a7e2694..d3b75d0 100644
--- a/src/core/common/random.hpp
+++ b/src/core/common/random.hpp
@@ -38,12 +38,70 @@
#include <stdint.h>
+#include <openthread/platform/crypto.h>
+
#include "common/debug.hpp"
#include "common/error.hpp"
-#include "common/random_manager.hpp"
+#include "common/non_copyable.hpp"
namespace ot {
namespace Random {
+
+/**
+ * This class manages random number generator initialization/deinitialization.
+ *
+ */
+class Manager : private NonCopyable
+{
+public:
+ /**
+ * This constructor initializes the object.
+ *
+ */
+ Manager(void);
+
+ /**
+ * This destructor deinitializes the object.
+ *
+ */
+ ~Manager(void);
+
+ /**
+ * This static method generates and returns a random value using a non-crypto Pseudo Random Number Generator.
+ *
+ * @returns A random `uint32_t` value.
+ *
+ */
+ static uint32_t NonCryptoGetUint32(void);
+
+#if !OPENTHREAD_RADIO
+ /**
+ * This static method fills a given buffer with cryptographically secure random bytes.
+ *
+ * @param[out] aBuffer A pointer to a buffer to fill with the random bytes.
+ * @param[in] aSize Size of buffer (number of bytes to fill).
+ *
+ * @retval kErrorNone Successfully filled buffer with random values.
+ *
+ */
+ static Error CryptoFillBuffer(uint8_t *aBuffer, uint16_t aSize) { return otPlatCryptoRandomGet(aBuffer, aSize); }
+#endif
+
+private:
+ class NonCryptoPrng // A non-crypto Pseudo Random Number Generator (PRNG)
+ {
+ public:
+ void Init(uint32_t aSeed);
+ uint32_t GetNext(void);
+
+ private:
+ uint32_t mState;
+ };
+
+ static uint16_t sInitCount;
+ static NonCryptoPrng sPrng;
+};
+
namespace NonCrypto {
/**
@@ -54,7 +112,7 @@
*/
inline uint32_t GetUint32(void)
{
- return ot::RandomManager::NonCryptoGetUint32();
+ return Manager::NonCryptoGetUint32();
}
/**
@@ -86,12 +144,9 @@
* @param[in] aMax A maximum value (this value is excluded from returned random result).
*
* @returns A random `uint8_t` value in the given range (i.e., aMin <= random value < aMax).
+ *
*/
-inline uint8_t GetUint8InRange(uint8_t aMin, uint8_t aMax)
-{
- OT_ASSERT(aMax > aMin);
- return (aMin + (GetUint8() % (aMax - aMin)));
-}
+uint8_t GetUint8InRange(uint8_t aMin, uint8_t aMax);
/**
* This function generates and returns a random `uint16_t` value within a given range `[aMin, aMax)`.
@@ -102,12 +157,9 @@
* @param[in] aMax A maximum value (this value is excluded from returned random result).
*
* @returns A random `uint16_t` value in the given range (i.e., aMin <= random value < aMax).
+ *
*/
-inline uint16_t GetUint16InRange(uint16_t aMin, uint16_t aMax)
-{
- OT_ASSERT(aMax > aMin);
- return (aMin + (GetUint16() % (aMax - aMin)));
-}
+uint16_t GetUint16InRange(uint16_t aMin, uint16_t aMax);
/**
* This function generates and returns a random `uint32_t` value within a given range `[aMin, aMax)`.
@@ -120,11 +172,7 @@
* @returns A random `uint32_t` value in the given range (i.e., aMin <= random value < aMax).
*
*/
-inline uint32_t GetUint32InRange(uint32_t aMin, uint32_t aMax)
-{
- OT_ASSERT(aMax > aMin);
- return (aMin + (GetUint32() % (aMax - aMin)));
-}
+uint32_t GetUint32InRange(uint32_t aMin, uint32_t aMax);
/**
* This function fills a given buffer with random bytes.
@@ -133,13 +181,7 @@
* @param[in] aSize Size of buffer (number of bytes to fill).
*
*/
-inline void FillBuffer(uint8_t *aBuffer, uint16_t aSize)
-{
- while (aSize-- != 0)
- {
- *aBuffer++ = GetUint8();
- }
-}
+void FillBuffer(uint8_t *aBuffer, uint16_t aSize);
/**
* This function adds a random jitter within a given range to a given value.
@@ -150,12 +192,7 @@
* @returns The given value with an added random jitter.
*
*/
-inline uint32_t AddJitter(uint32_t aValue, uint16_t aJitter)
-{
- aJitter = (aJitter <= aValue) ? aJitter : static_cast<uint16_t>(aValue);
-
- return aValue + GetUint32InRange(0, 2 * aJitter + 1) - aJitter;
-}
+uint32_t AddJitter(uint32_t aValue, uint16_t aJitter);
} // namespace NonCrypto
@@ -174,7 +211,7 @@
*/
inline Error FillBuffer(uint8_t *aBuffer, uint16_t aSize)
{
- return RandomManager::CryptoFillBuffer(aBuffer, aSize);
+ return Manager::CryptoFillBuffer(aBuffer, aSize);
}
} // namespace Crypto
diff --git a/src/core/common/random_manager.hpp b/src/core/common/random_manager.hpp
deleted file mode 100644
index adc4d09..0000000
--- a/src/core/common/random_manager.hpp
+++ /dev/null
@@ -1,105 +0,0 @@
-/*
- * Copyright (c) 2019, The OpenThread Authors.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the copyright holder nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-/**
- * @file
- * This file includes definitions for OpenThread random number generation manager class.
- */
-
-#ifndef RANDOM_MANAGER_HPP_
-#define RANDOM_MANAGER_HPP_
-
-#include "openthread-core-config.h"
-
-#include <stdint.h>
-
-#include <openthread/platform/crypto.h>
-
-#include "common/error.hpp"
-#include "common/non_copyable.hpp"
-
-namespace ot {
-
-/**
- * This class manages random number generator initialization/deinitialization.
- *
- */
-class RandomManager : private NonCopyable
-{
-public:
- /**
- * This constructor initializes the object.
- *
- */
- RandomManager(void);
-
- /**
- * This destructor deinitializes the object.
- *
- */
- ~RandomManager(void);
-
- /**
- * This static method generates and returns a random value using a non-crypto Pseudo Random Number Generator.
- *
- * @returns A random `uint32_t` value.
- *
- */
- static uint32_t NonCryptoGetUint32(void);
-
-#if !OPENTHREAD_RADIO
- /**
- * This static method fills a given buffer with cryptographically secure random bytes.
- *
- * @param[out] aBuffer A pointer to a buffer to fill with the random bytes.
- * @param[in] aSize Size of buffer (number of bytes to fill).
- *
- * @retval kErrorNone Successfully filled buffer with random values.
- *
- */
- static Error CryptoFillBuffer(uint8_t *aBuffer, uint16_t aSize) { return otPlatCryptoRandomGet(aBuffer, aSize); }
-#endif
-
-private:
- class NonCryptoPrng // A non-crypto Pseudo Random Number Generator (PRNG)
- {
- public:
- void Init(uint32_t aSeed);
- uint32_t GetNext(void);
-
- private:
- uint32_t mState;
- };
-
- static uint16_t sInitCount;
- static NonCryptoPrng sPrng;
-};
-
-} // namespace ot
-
-#endif // RANDOM_MANAGER_HPP_