From 082aefc270ab780c2db8619eb2d5c7a2a1bac093 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Mon, 18 Apr 2022 15:37:46 -0700 Subject: [PATCH] [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`) --- Android.mk | 2 +- src/core/BUILD.gn | 5 +- src/core/CMakeLists.txt | 4 +- src/core/Makefile.am | 5 +- src/core/common/instance.hpp | 6 +- .../common/{random_manager.cpp => random.cpp} | 59 ++++++++-- src/core/common/random.hpp | 99 +++++++++++------ src/core/common/random_manager.hpp | 105 ------------------ 8 files changed, 127 insertions(+), 158 deletions(-) rename src/core/common/{random_manager.cpp => random.cpp} (72%) delete mode 100644 src/core/common/random_manager.hpp diff --git a/Android.mk b/Android.mk index ce27a3be1..b144b5402 100644 --- a/Android.mk +++ b/Android.mk @@ -238,7 +238,7 @@ LOCAL_SRC_FILES := \ 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 cb8d0776f..e5527a627 100644 --- a/src/core/BUILD.gn +++ b/src/core/BUILD.gn @@ -423,9 +423,8 @@ openthread_core_files = [ "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 @@ openthread_radio_sources = [ "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 f6e9f1b7b..d1b1d71ae 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -104,7 +104,7 @@ set(COMMON_SOURCES 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 @@ set(RADIO_COMMON_SOURCES 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 a607faa2f..2d1e77618 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -194,7 +194,7 @@ SOURCES_COMMON = \ 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 @@ RADIO_SOURCES_COMMON = \ 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 @@ HEADERS_COMMON = \ 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 a184a09b1..d49b6c063 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 @@ private: #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 @@ private: 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 e059bdeeb..482d68d75 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 #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 @@ exit: sInitCount++; } -RandomManager::~RandomManager(void) +Manager::~Manager(void) { OT_ASSERT(sInitCount > 0); @@ -81,7 +80,7 @@ exit: return; } -uint32_t RandomManager::NonCryptoGetUint32(void) +uint32_t Manager::NonCryptoGetUint32(void) { OT_ASSERT(sInitCount > 0); @@ -91,7 +90,7 @@ uint32_t RandomManager::NonCryptoGetUint32(void) //------------------------------------------------------------------- // 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 @@ void RandomManager::NonCryptoPrng::Init(uint32_t aSeed) 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 @@ uint32_t RandomManager::NonCryptoPrng::GetNext(void) 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(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 a7e269453..d3b75d0f2 100644 --- a/src/core/common/random.hpp +++ b/src/core/common/random.hpp @@ -38,12 +38,70 @@ #include +#include + #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 @@ namespace NonCrypto { */ inline uint32_t GetUint32(void) { - return ot::RandomManager::NonCryptoGetUint32(); + return Manager::NonCryptoGetUint32(); } /** @@ -86,12 +144,9 @@ inline uint16_t GetUint16(void) * @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 @@ inline uint8_t GetUint8InRange(uint8_t aMin, uint8_t aMax) * @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 @@ inline uint16_t GetUint16InRange(uint16_t aMin, uint16_t aMax) * @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 @@ inline uint32_t GetUint32InRange(uint32_t aMin, uint32_t aMax) * @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 @@ inline void FillBuffer(uint8_t *aBuffer, uint16_t aSize) * @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(aValue); - - return aValue + GetUint32InRange(0, 2 * aJitter + 1) - aJitter; -} +uint32_t AddJitter(uint32_t aValue, uint16_t aJitter); } // namespace NonCrypto @@ -174,7 +211,7 @@ namespace Crypto { */ 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 adc4d09ef..000000000 --- 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 - -#include - -#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_