RNG.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-2022, Texas Instruments Incorporated
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
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*!****************************************************************************
33  * @file RNG.h
34  *
35  * @brief RNG driver header
36  *
37  * @anchor ti_drivers_RNG_Overview
38  * # Overview #
39  * The Random Number Generator (RNG) module generates random data of variable
40  * lengths from a pool of entropy. The pool of entropy is maintained by the
41  * driver using implementation-specific sources of entropy.
42  * The output is suitable for applications requiring cryptographically
43  * random data such as keying material for private or symmetric keys.
44  *
45  * @anchor ti_drivers_RNG_Usage
46  * # Usage #
47  *
48  * ## Initialization ##
49  * Unlike most drivers, there is a global instance of RNG driver data
50  * that is always available once #RNG_init() is called. This data will contain
51  * the entropy pool and any needed state information required to refill the
52  * pool. #RNG_init() should be called once before using other RNG
53  * driver APIs.
54  *
55  * @note Some implementations restrict when RNG_init() may be called.
56  * Check the implementation's documentation for more information.
57  *
58  * ## Before starting a RNG operation ##
59  *
60  * Before starting a RNG operation, the application must do the following:
61  * - Call RNG_init() to initialize the driver's global instance data.
62  * - Call RNG_Params_init() to initialize the RNG_Params to default values.
63  * - Modify the RNG_Params as desired.
64  * - Call RNG_open() to open an instance of the driver.
65  *
66  * @note Some implementations restrict when RNG_init() may be called.
67  * Check the implementation's documentation for more information.
68  *
69  * ## Entropy Pool Management ##
70  *
71  * At any time after calling RNG_init(), the application may call
72  * RNG_fillPoolIfLessThan() to add entropy to the pool which will then make
73  * future requests for entropy execute faster. Note that the driver never
74  * automatically refills the pool. However, if the pool is empty, the RNG
75  * driver will still generate entropy upon request (for example when
76  * RNG_getRandomBits() is called).
77  *
78  * The application is responsible for deciding when it is appropriate to
79  * spend the time and energy to refill the pool. One suggested location
80  * to do so is the idle thread.
81  *
82  * ## RNG operations ##
83  *
84  * Use RNG_getRandomBits() to obtain random bits from the entropy pool and
85  * copy them to a buffer/array. The caller must allocate memory sufficient
86  * to hold at least the number of bits of random data requested.
87  *
88  * ## After the RNG operation completes ##
89  *
90  * After the RNG operation completes, the application should either start
91  * another operation or close the driver by calling RNG_close(). Note that the
92  * singleton instance of the driver, along with its associated pool of entropy
93  * will still exist and will be used by any future RNG_open() calls. Note that
94  * closing the driver instance may not be strictly required, but is good
95  * practice.
96  *
97  * ## Security ##
98  *
99  * ### Data Protection ###
100  *
101  * The entropy pool and any required state to generate more entropy is
102  * maintained in memory, in the driver's global instance data. The entirety of
103  * this data is stored in two global variables called RNG_instanceData and
104  * RNG_instancePool. It is up to the system to provide adequate
105  * protection (primarily confidentiality and integrity) of these in-memory
106  * assets.
107  *
108  * ### Timing Side Channels ###
109  *
110  * Functions which provide for generation of a value within a range use
111  * an algorithm which is timing-constant when the following parameters
112  * are held constant: lowerLimit, upperLimit, bitLength,
113  * and endianess. Thus, while the driver may create multiple candidates for the
114  * value to find one within the range, timing will not leak the final
115  * value's relation to the limits. However, timing may leak the bitLength,
116  * the endianess, and the use of #CryptoUtils_limitZero, #CryptoUtils_limitOne,
117  * or NULL for the limit values.
118  *
119  * @anchor ti_drivers_RNG_Synopsis
120  * ## Synopsis
121  * @anchor ti_drivers_RNG_Synopsis_Code
122  * ### Generate random bytes to a user provided buffer #
123  *
124  * @code
125  *
126  * #include <ti/drivers/RNG.h>
127  * #include "ti_drivers_config.h"
128  *
129  * // Setup RNG
130  * RNG_Init();
131  * RNG_fillPoolIfLessThan(RNG_POOL_BYTE_SIZE);
132  *
133  * // Use RNG
134  * #define RANDOM_BYTES_SIZE 16u
135  * RNG_Handle handle;
136  * int_fast16_t result;
137  *
138  * uint8_t randomBytesArray[RANDOM_BYTES_SIZE] = {0};
139  *
140  * handle = RNG_open(0, NULL);
141  *
142  * if (!handle) {
143  * // Handle error
144  * while(1);
145  * }
146  *
147  * result = RNG_getRandomBits(handle, randomBytesArray, RANDOM_BYTES_SIZE * 8);
148  *
149  * if (result != RNG_STATUS_SUCCESS) {
150  * // Handle error
151  * while(1);
152  * }
153  *
154  * RNG_close(handle);
155  *
156  * // Refill RNG Pool when convenient
157  * RNG_fillPoolIfLessThan(RNG_POOL_BYTE_SIZE);
158  * @endcode
159  *
160  * @anchor ti_drivers_RNG_Examples
161  * ## Examples
162  *
163  * The following examples do not show the process of initializing the RNG
164  * module and refilling the pool.
165  * See @ref ti_drivers_RNG_Synopsis RNG Driver Synopsis for an example
166  * showing those parts of RNG operation. *
167  *
168  * ### Generate a number within a range ###
169  *
170  * @code
171  *
172  * #include <ti/drivers/RNG.h>
173  *
174  * #define RANDOM_BIT_SIZE 15u
175  * #define RANDOM_BYTE_SIZE ((RANDOM_BIT_SIZE + 7u)/8u)
176  *
177  * RNG_Handle handle;
178  * int_fast16_t result;
179  *
180  * uint8_t randomBytesArray[RANDOM_BYTES_SIZE] = {0};
181  * uint8_t upperLimit[RANDOM_BYTES_SIZE] = {0xA9, 0x61}; // 25,001, LE format
182  *
183  * handle = RNG_open(0, NULL);
184  *
185  * if (!handle) {
186  * // Handle error
187  * while(1);
188  * }
189  *
190  * // Generate a number from 1 to 25,000 (inclusive)
191  * // Note that lowerLimit parameter is inclusive and upperLimit is
192  * // exclusive. Thus, upperLimit is set to 25,001.
193  * result = RNG_getLERandomNumberInRange(RNG_Handle handle, RNG_limitOne,
194  * upperLimit, randomBytesArray,
195  * RANDOM_BIT_SIZE);
196  *
197  *
198  * if (result != RNG_STATUS_SUCCESS) {
199  * // Handle error
200  * while(1);
201  * }
202  *
203  * RNG_close(handle);
204  *
205  * @endcode
206  *
207  *
208  * ### Generate an ECC private key ###
209  *
210  * @code
211  *
212  * #include <ti/drivers/RNG.h>
213  * #include <ti/drivers/cryptoutils/ecc/ECCParams.h>
214  *
215  * // Values are chosen to generate a NIST 256 bit key.
216  * CryptoKey privateKey;
217  * uint8_t privateKeyingMaterial[NISTP256_PARAM_SIZE_BYTES];
218  * RNG_Handle handle;
219  * int_fast16_t result;
220  *
221  * handle = RNG_open(0, NULL);
222  *
223  * if (!handle) {
224  * // Handle error
225  * while(1);
226  * }
227  *
228  * CryptoKeyPlaintext_initBlankKey(&privateKey, privateKeyingMaterial,
229  * ECCParams_NISTP256.length);
230  *
231  * // Generate NIST 256 bit key in BE format.
232  * result = RNG_generateBEKeyInRange(RNG_Handle handle, RNG_limitOne,
233  * ECCParams_NISTP256.order, privateKey,
234  * 256);
235  *
236  *
237  * if (result != RNG_STATUS_SUCCESS) {
238  * // Handle error
239  * while(1);
240  * }
241  *
242  * RNG_close(handle);
243  *
244  * @endcode
245  *
246  */
247 
248 #ifndef ti_drivers_RNG__include
249 #define ti_drivers_RNG__include
250 
251 #include <stdbool.h>
252 #include <stddef.h>
253 #include <stdint.h>
254 
256 
257 #ifdef __cplusplus
258 extern "C" {
259 #endif
260 
273 #define RNG_STATUS_RESERVED (-32)
274 
281 #define RNG_STATUS_SUCCESS ((int_fast16_t)0)
282 
289 #define RNG_STATUS_ERROR ((int_fast16_t)-1)
290 
300 #define RNG_STATUS_RESOURCE_UNAVAILABLE ((int_fast16_t)-2)
301 
307 #define RNG_STATUS_INVALID_INPUTS ((int_fast16_t)-3)
308 
312 #define RNG_STATUS_CANCELED ((int_fast16_t)-4)
313 
319 #define RNG_ENTROPY_EXHAUSTED ((int_fast16_t)-5)
320 
326 #define RNG_STATUS_INIT_NOT_ALLOWED ((int_fast16_t)-6)
327 
331 #define RNG_MAX_BIT_LENGTH ((size_t)1u << 20u) /* 1 MiB */
332 
344 typedef struct
345 {
347  void *object;
348 
350  void const *hwAttrs;
351 } RNG_Config;
352 
356 typedef const RNG_Config *RNG_Handle;
357 
382 typedef enum
383 {
401 
417 typedef void (*RNG_CryptoKeyCallbackFxn)(RNG_Handle handle, int_fast16_t returnValue, CryptoKey *key);
418 
437 typedef void (*RNG_RandomBitsCallbackFxn)(RNG_Handle handle,
438  int_fast16_t returnValue,
439  uint8_t *randomBits,
440  size_t randomBitsLength);
441 
454 typedef struct
455 {
466  uint32_t timeout;
469 } RNG_Params;
470 
476 extern const RNG_Params RNG_defaultParams;
477 
481 extern const size_t RNG_poolByteSize;
482 
496 int_fast16_t RNG_init(void);
497 
517 int_fast16_t RNG_fillPoolIfLessThan(size_t bytes);
518 
533 
551 RNG_Handle RNG_open(uint_least8_t index, const RNG_Params *params);
552 
562 void RNG_close(RNG_Handle handle);
563 
600 int_fast16_t RNG_getRandomBits(RNG_Handle handle, void *randomBits, size_t randomBitsLength);
601 
654 int_fast16_t RNG_getLERandomNumberInRange(RNG_Handle handle,
655  const void *lowerLimit,
656  const void *upperLimit,
657  void *randomNumber,
658  size_t randomNumberBitLength);
659 
712 int_fast16_t RNG_getBERandomNumberInRange(RNG_Handle handle,
713  const void *lowerLimit,
714  const void *upperLimit,
715  void *randomNumber,
716  size_t randomNumberBitLength);
717 
748 int_fast16_t RNG_generateKey(RNG_Handle handle, CryptoKey *key);
749 
799 int_fast16_t RNG_generateLEKeyInRange(RNG_Handle handle,
800  const void *lowerLimit,
801  const void *upperLimit,
802  CryptoKey *key,
803  size_t randomNumberBitLength);
804 
854 int_fast16_t RNG_generateBEKeyInRange(RNG_Handle handle,
855  const void *lowerLimit,
856  const void *upperLimit,
857  CryptoKey *key,
858  size_t randomNumberBitLength);
859 
883 RNG_Handle RNG_construct(const RNG_Config *config, const RNG_Params *params);
884 
904 int_fast16_t RNG_cancelOperation(RNG_Handle handle);
905 
906 #ifdef __cplusplus
907 }
908 #endif
909 
910 #endif /* ti_drivers_RNG__include */
ADC_Params params
Definition: Driver_Init.h:11
const RNG_Params RNG_defaultParams
Default RNG_Params structure.
The CryptoKey type is an opaque representation of a cryptographic key.
RNG_ReturnBehavior returnBehavior
Definition: RNG.h:456
int_fast16_t RNG_init(void)
This function initializes the RNG module.
RNG_RandomBitsCallbackFxn randomBitsCallbackFxn
Definition: RNG.h:461
int_fast16_t RNG_generateKey(RNG_Handle handle, CryptoKey *key)
Generate random bits and output them to the given CryptoKey object.
const size_t RNG_poolByteSize
The byte size of the pool.
void * object
Definition: RNG.h:347
CryptoKey datastructure.
Definition: CryptoKey.h:198
Definition: RNG.h:391
int_fast16_t RNG_generateBEKeyInRange(RNG_Handle handle, const void *lowerLimit, const void *upperLimit, CryptoKey *key, size_t randomNumberBitLength)
Generate random number, stored in big-endian (BE) format, where the number is within the specified ra...
RNG_ReturnBehavior
The way in which RNG function calls return after generating the requested entropy.
Definition: RNG.h:382
RNG_Handle RNG_construct(const RNG_Config *config, const RNG_Params *params)
Constructs a new RNG object.
int_fast16_t RNG_getBERandomNumberInRange(RNG_Handle handle, const void *lowerLimit, const void *upperLimit, void *randomNumber, size_t randomNumberBitLength)
Generate random number, stored in big-endian (BE) format, where the number is within the specified ra...
RNG_CryptoKeyCallbackFxn cryptoKeyCallbackFxn
Definition: RNG.h:457
void(* RNG_RandomBitsCallbackFxn)(RNG_Handle handle, int_fast16_t returnValue, uint8_t *randomBits, size_t randomBitsLength)
The definition of a callback function used by the RNG driver when RNG_getRandomBits(), RNG_getLERandomNumberInRange(), or RNG_getBERandomNumberInRange is called with RNG_RETURN_BEHAVIOR_CALLBACK.
Definition: RNG.h:437
void RNG_close(RNG_Handle handle)
Function to close a RNG peripheral specified by the RNG handle.
int_fast16_t RNG_fillPoolIfLessThan(size_t bytes)
Fills the pool with entropy if the number of bytes with entropy in the pool is less than the value sp...
void const * hwAttrs
Definition: RNG.h:350
uint32_t timeout
Definition: RNG.h:466
int_fast16_t RNG_getLERandomNumberInRange(RNG_Handle handle, const void *lowerLimit, const void *upperLimit, void *randomNumber, size_t randomNumberBitLength)
Generate random number, stored in little-endian (LE) format, where the number is within the specified...
int_fast16_t RNG_getRandomBits(RNG_Handle handle, void *randomBits, size_t randomBitsLength)
Generate random bits and output to the given array.
Definition: RNG.h:384
int_fast16_t RNG_cancelOperation(RNG_Handle handle)
Aborts an ongoing RNG operation and clears internal buffers.
int_fast16_t RNG_generateLEKeyInRange(RNG_Handle handle, const void *lowerLimit, const void *upperLimit, CryptoKey *key, size_t randomNumberBitLength)
Generate random number, in little-endian (LE) format, where the number is within the specified range...
RNG_Handle RNG_open(uint_least8_t index, const RNG_Params *params)
This function opens a given RNG peripheral.
RNG Parameters.
Definition: RNG.h:454
RNG Global configuration.
Definition: RNG.h:344
void RNG_Params_init(RNG_Params *params)
Function to initialize the RNG_Params struct to its defaults.
const RNG_Config * RNG_Handle
A handle that is returned from a RNG_open() call.
Definition: RNG.h:356
Definition: RNG.h:395
void(* RNG_CryptoKeyCallbackFxn)(RNG_Handle handle, int_fast16_t returnValue, CryptoKey *key)
The definition of a callback function used by the RNG driver when RNG_generateKey(), RNG_generateLEKeyInRange(), or RNG_generateBEKeyInRange() is called with RNG_RETURN_BEHAVIOR_CALLBACK.
Definition: RNG.h:417
© Copyright 1995-2022, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale