1 /*
2 * Copyright (c) 2019, The OpenThread Authors.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /**
30 * @file
31 * This file includes definitions for OpenThread random number generation.
32 */
33
34 #ifndef RANDOM_HPP_
35 #define RANDOM_HPP_
36
37 #include "openthread-core-config.h"
38
39 #include <stdint.h>
40
41 #include <openthread/platform/crypto.h>
42
43 #include "common/debug.hpp"
44 #include "common/error.hpp"
45 #include "common/non_copyable.hpp"
46 #include "common/type_traits.hpp"
47
48 namespace ot {
49 namespace Random {
50
51 /**
52 * Manages random number generator initialization/deinitialization.
53 *
54 */
55 class Manager : private NonCopyable
56 {
57 public:
58 /**
59 * Initializes the object.
60 *
61 */
62 Manager(void);
63
64 /**
65 * This destructor deinitializes the object.
66 *
67 */
68 ~Manager(void);
69
70 /**
71 * Generates and returns a random value using a non-crypto Pseudo Random Number Generator.
72 *
73 * @returns A random `uint32_t` value.
74 *
75 */
76 static uint32_t NonCryptoGetUint32(void);
77
78 #if !OPENTHREAD_RADIO
79 /**
80 * Fills a given buffer with cryptographically secure random bytes.
81 *
82 * @param[out] aBuffer A pointer to a buffer to fill with the random bytes.
83 * @param[in] aSize Size of buffer (number of bytes to fill).
84 *
85 * @retval kErrorNone Successfully filled buffer with random values.
86 *
87 */
CryptoFillBuffer(uint8_t * aBuffer,uint16_t aSize)88 static Error CryptoFillBuffer(uint8_t *aBuffer, uint16_t aSize) { return otPlatCryptoRandomGet(aBuffer, aSize); }
89 #endif
90
91 private:
92 class NonCryptoPrng // A non-crypto Pseudo Random Number Generator (PRNG)
93 {
94 public:
95 void Init(uint32_t aSeed);
96 uint32_t GetNext(void);
97
98 private:
99 uint32_t mState;
100 };
101
102 static uint16_t sInitCount;
103 static NonCryptoPrng sPrng;
104 };
105
106 namespace NonCrypto {
107
108 /**
109 * Generates and returns a random `uint32_t` value.
110 *
111 * @returns A random `uint32_t` value.
112 *
113 */
GetUint32(void)114 inline uint32_t GetUint32(void) { return Manager::NonCryptoGetUint32(); }
115
116 /**
117 * Generates and returns a random byte.
118 *
119 * @returns A random `uint8_t` value.
120 *
121 */
GetUint8(void)122 inline uint8_t GetUint8(void) { return static_cast<uint8_t>(GetUint32() & 0xff); }
123
124 /**
125 * Generates and returns a random `uint16_t` value.
126 *
127 * @returns A random `uint16_t` value.
128 *
129 */
GetUint16(void)130 inline uint16_t GetUint16(void) { return static_cast<uint16_t>(GetUint32() & 0xffff); }
131
132 /**
133 * Generates and returns a random `uint8_t` value within a given range `[aMin, aMax)`.
134 *
135 * @param[in] aMin A minimum value (this value can be included in returned random result).
136 * @param[in] aMax A maximum value (this value is excluded from returned random result).
137 *
138 * @returns A random `uint8_t` value in the given range (i.e., aMin <= random value < aMax).
139 *
140 */
141 uint8_t GetUint8InRange(uint8_t aMin, uint8_t aMax);
142
143 /**
144 * Generates and returns a random `uint16_t` value within a given range `[aMin, aMax)`.
145 *
146 * @note The returned random value can include the @p aMin value but excludes the @p aMax.
147 *
148 * @param[in] aMin A minimum value (this value can be included in returned random result).
149 * @param[in] aMax A maximum value (this value is excluded from returned random result).
150 *
151 * @returns A random `uint16_t` value in the given range (i.e., aMin <= random value < aMax).
152 *
153 */
154 uint16_t GetUint16InRange(uint16_t aMin, uint16_t aMax);
155
156 /**
157 * Generates and returns a random `uint32_t` value within a given range `[aMin, aMax)`.
158 *
159 * @note The returned random value can include the @p aMin value but excludes the @p aMax.
160 *
161 * @param[in] aMin A minimum value (this value can be included in returned random result).
162 * @param[in] aMax A maximum value (this value is excluded from returned random result).
163 *
164 * @returns A random `uint32_t` value in the given range (i.e., aMin <= random value < aMax).
165 *
166 */
167 uint32_t GetUint32InRange(uint32_t aMin, uint32_t aMax);
168
169 /**
170 * Fills a given buffer with random bytes.
171 *
172 * @param[out] aBuffer A pointer to a buffer to fill with the random bytes.
173 * @param[in] aSize Size of buffer (number of bytes to fill).
174 *
175 */
176 void FillBuffer(uint8_t *aBuffer, uint16_t aSize);
177
178 /**
179 * Fills a given object with random bytes.
180 *
181 * @tparam ObjectType The object type to fill.
182 *
183 * @param[in] aObject A reference to the object to fill.
184 *
185 */
Fill(ObjectType & aObject)186 template <typename ObjectType> void Fill(ObjectType &aObject)
187 {
188 static_assert(!TypeTraits::IsPointer<ObjectType>::kValue, "ObjectType must not be a pointer");
189
190 FillBuffer(reinterpret_cast<uint8_t *>(&aObject), sizeof(ObjectType));
191 }
192
193 /**
194 * Adds a random jitter within a given range to a given value.
195 *
196 * @param[in] aValue A value to which the random jitter is added.
197 * @param[in] aJitter Maximum jitter. Random jitter is selected from the range `[-aJitter, aJitter]`.
198 *
199 * @returns The given value with an added random jitter.
200 *
201 */
202 uint32_t AddJitter(uint32_t aValue, uint16_t aJitter);
203
204 } // namespace NonCrypto
205
206 #if !OPENTHREAD_RADIO
207
208 namespace Crypto {
209
210 /**
211 * Fills a given buffer with cryptographically secure random bytes.
212 *
213 * @param[out] aBuffer A pointer to a buffer to fill with the random bytes.
214 * @param[in] aSize Size of buffer (number of bytes to fill).
215 *
216 * @retval kErrorNone Successfully filled buffer with random values.
217 *
218 */
FillBuffer(uint8_t * aBuffer,uint16_t aSize)219 inline Error FillBuffer(uint8_t *aBuffer, uint16_t aSize) { return Manager::CryptoFillBuffer(aBuffer, aSize); }
220
221 /**
222 * Fills a given object with cryptographically secure random bytes.
223 *
224 * @tparam ObjectType The object type to fill.
225 *
226 * @param[in] aObject A reference to the object to fill.
227 *
228 * @retval kErrorNone Successfully filled @p aObject with random values.
229 * @retval kErrorFailed Failed to generate secure random bytes to fill the object.
230 *
231 */
Fill(ObjectType & aObject)232 template <typename ObjectType> Error Fill(ObjectType &aObject)
233 {
234 static_assert(!TypeTraits::IsPointer<ObjectType>::kValue, "ObjectType must not be a pointer");
235
236 return FillBuffer(reinterpret_cast<uint8_t *>(&aObject), sizeof(ObjectType));
237 }
238
239 } // namespace Crypto
240
241 #endif // !OPENTHREAD_RADIO
242
243 } // namespace Random
244 } // namespace ot
245
246 #endif // RANDOM_HPP_
247