TRNG.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2024, 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 TRNG.h
34  *
35  * @brief TRNG driver header
36  *
37  * @anchor ti_drivers_TRNG_Overview
38  * # Overview #
39  * The True Random Number Generator (TRNG) module generates random data of variable
40  * lengths from a source of entropy. The output is suitable for applications
41  * requiring cryptographically random data such as keying material for
42  * private or symmetric keys.
43  *
44  * @anchor ti_drivers_TRNG_Usage
45  * # Usage #
46  *
47  * ## Before starting a TRNG operation #
48  *
49  * Before starting a TRNG operation, the application must do the following:
50  * - Call TRNG_init() to initialize the driver.
51  * - Call TRNG_Params_init() to initialize the TRNG_Params to default values.
52  * - Modify the TRNG_Params as desired.
53  * - Call TRNG_open() to open an instance of the driver.
54  * - Option 1: Use TRNG_generateKey() that writes random bytes to a CryptoKey. <br>
55  * Initialize a blank CryptoKey. These opaque data structures are representations
56  * of keying material and its storage. Depending on how the keying material
57  * is stored (RAM or flash, key store), the CryptoKey must be
58  * initialized differently. The TRNG API can handle all types of CryptoKey.
59  * However, not all device-specific implementations support all types of CryptoKey.
60  * Devices without a key store will not support CryptoKeys with keying material
61  * stored in a key store for example.
62  * All devices support plaintext CryptoKeys.
63  * - Option 2: Use TRNG_getRandomBytes() that writes random bytes to a buffer. <br>
64  * Allocate memory sufficient to hold the number of bytes of random data requested.
65  *
66  * ## TRNG operations #
67  *
68  * TRNG_generateKey() provides the most basic functionality. Use it to
69  * generate key-material of a specified size. An example use-case would be generating
70  * a symmetric key for AES encryption and / or authentication. If entropy data is needed
71  * for anything other than a key-material, use TRNG_getRandomBytes() that
72  * writes random bytes from the entropy source to a buffer/array.
73  *
74  * For CC27XX, the CRNG is the default configuration for the NRBG engine.
75  * Use TRNGLPF3HSM_switchNrbgMode() to switch between CRNG and TRNG configurations.
76  *
77  * For CC27XX devices only, the TRNG driver accepts two types of cryptoKey encoding:
78  * - CryptoKey_BLANK_PLAINTEXT
79  * - CryptoKey_BLANK_PLAINTEXT_HSM
80  *
81  * To generate an ECC private key, you should use rejection sampling to ensure
82  * that the keying material is in the interval [1, n - 1]. The ECDH public key
83  * generation APIs will reject private keys that are outside of this interval.
84  * This information may be used to generate keying material until a suitable
85  * key is generated. For most curves, it is improbable to generate a random number
86  * outside of this interval because n is a large number close to the maximum
87  * number that would fit in the k-byte keying material array. An example
88  * of how to do this is given below.
89  *
90  * ## TRNG Driver Limitation for CC27XX devices #
91  *
92  * For CC27XX devices, the underlying HSM engine for which the driver gets its source of
93  * entropy and random numbers from, you have the ability to configure the NRBG
94  * (Non-deterministic Random Bits Generator) engine which is part of the HSM in either
95  * CRNG or TRNG modes. Please refer to the device specific header file for a
96  * definition of both methods.
97  *
98  * In addition, the size of random data requested must be a 32-bit multiple. The
99  * appropriate error code, TRNG_STATUS_INVALID_INPUT_SIZE, will be returned to the user in
100  * the case this rule is not adhered to and the input size is not a multiple of 4 bytes, (32-bits).
101  *
102  * For example, in the case you are requesting a 521-bit (66 bytes) key for a
103  * SEC_P_521 related asymmetric key operations, the user must provide a buffer and a input size
104  * that rounds up to the next (32-bit) aligned byte (68 Bytes) and therefore,
105  * adheres to the above limitation.
106  *
107  * ## After the TRNG operation completes #
108  *
109  * After the TRNG operation completes, the application should either start another operation
110  * or close the driver by calling TRNG_close().
111  *
112  * @anchor ti_drivers_TRNG_Synopsis
113  * ## Synopsis
114  * @anchor ti_drivers_TRNG_Synopsis_Code
115  * @code
116  * // Import TRNG Driver definitions
117  * #include <ti/drivers/TRNG.h>
118  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
119  *
120  * // Define name for TRNG channel index
121  * #define TRNG_INSTANCE 0
122  *
123  * #define KEY_LENGTH_BYTES 16
124  *
125  * TRNG_init();
126  *
127  * handle = TRNG_open(TRNG_INSTANCE, NULL);
128  *
129  * CryptoKeyPlaintext_initBlankKey(&entropyKey, entropyBuffer, KEY_LENGTH_BYTES);
130  *
131  * result = TRNG_generateKey(handle, &entropyKey);
132  *
133  * TRNG_close(handle);
134  *
135  * @endcode
136  *
137  * @anchor ti_drivers_TRNG_Examples
138  * ## Examples
139  *
140  * ### TRNG force a reseed (for CC27XX devices only) #
141  *
142  * This example code for reseeding the DRBG engine is only applicable for CC27XX
143  * devices. Other devices and their corresponding SDKs do not have this API.
144  *
145  * @code
146  *
147  * #include <ti/drivers/TRNG.h>
148  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
149  *
150  * #define KEY_LENGTH_BYTES 16
151  *
152  * TRNG_Handle handle;
153  * int_fast16_t result;
154  *
155  * uint8_t randomBytesArray[RANDOM_BYTES_SIZE] = {0};
156  *
157  * handle = TRNG_open(0, NULL);
158  *
159  * if (!handle) {
160  * // Handle error
161  * while(1);
162  * }
163  *
164  * result = TRNG_getRandomBytes(handle, randomBytesArray, RANDOM_BYTES_SIZE);
165  *
166  * if (result != TRNG_STATUS_SUCCESS) {
167  * // Handle error
168  * while(1);
169  * }
170  *
171  * result = TRNGLPF3HSM_reseedHSM(handle);
172  *
173  * if (result != TRNG_STATUS_SUCCESS) {
174  * // Handle error
175  * while(1);
176  * }
177  *
178  * result = TRNG_getRandomBytes(handle, randomBytesArray, RANDOM_BYTES_SIZE);
179  *
180  * if (result != TRNG_STATUS_SUCCESS) {
181  * // Handle error
182  * while(1);
183  * }
184  *
185  * TRNG_close(handle);
186  *
187  * @endcode
188  *
189  *
190  * ### TRNG switch NRBG engine (for CC27XX devices only) #
191  *
192  * !!!!!!!!!!!!! WARNING !!!!!!!!!!!!!
193  * This example code is only applicable for CC27XX devices.
194  * Other devices and their corresponding SDKs do not have this API.
195  *
196  * @code
197  *
198  * #include <ti/drivers/TRNG.h>
199  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
200  *
201  * #define KEY_LENGTH_BYTES 16
202  *
203  * TRNG_Handle handle;
204  * int_fast16_t result;
205  *
206  * uint8_t randomBytesArray[RANDOM_BYTES_SIZE] = {0};
207  *
208  * // The driver is initialized with CRNG configuration by default.
209  * handle = TRNG_open(0, NULL);
210  *
211  * if (!handle) {
212  * // Handle error
213  * while(1);
214  * }
215  *
216  * result = TRNG_getRandomBytes(handle, randomBytesArray, RANDOM_BYTES_SIZE);
217  *
218  * if (result != TRNG_STATUS_SUCCESS) {
219  * // Handle error
220  * while(1);
221  * }
222  *
223  * result = TRNGLPF3HSM_switchNrbgMode(handle, TRNG_MODE_TRNG);
224  *
225  * if (result != TRNG_STATUS_SUCCESS) {
226  * // Handle error
227  * while(1);
228  * }
229  *
230  * result = TRNG_getRandomBytes(handle, randomBytesArray, RANDOM_BYTES_SIZE);
231  *
232  * if (result != TRNG_STATUS_SUCCESS) {
233  * // Handle error
234  * while(1);
235  * }
236  *
237  * TRNG_close(handle);
238  *
239  * @endcode
240  *
241  * ### Generate symmetric encryption key #
242  *
243  * @code
244  *
245  * #include <ti/drivers/TRNG.h>
246  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
247  *
248  * #define KEY_LENGTH_BYTES 16
249  *
250  * TRNG_Handle handle;
251  * int_fast16_t result;
252  *
253  * CryptoKey entropyKey;
254  * uint8_t entropyBuffer[KEY_LENGTH_BYTES] = {0};
255  *
256  * handle = TRNG_open(0, NULL);
257  *
258  * if (!handle) {
259  * // Handle error
260  * while(1);
261  * }
262  *
263  * CryptoKeyPlaintext_initBlankKey(&entropyKey, entropyBuffer, KEY_LENGTH_BYTES);
264  *
265  * result = TRNG_generateKey(handle, &entropyKey);
266  *
267  * if (result != TRNG_STATUS_SUCCESS) {
268  * // Handle error
269  * while(1);
270  * }
271  *
272  * TRNG_close(handle);
273  *
274  * @endcode
275  *
276  * ### Generate ECC private and public key using rejection sampling #
277  *
278  * @code
279  *
280  * #include <ti/drivers/TRNG.h>
281  * #include <ti/drivers/ECDH.h>
282  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
283  * #include <ti/drivers/cryptoutils/ecc/ECCParams.h>
284  *
285  * TRNG_Handle trngHandle;
286  * ECDH_Handle ecdhHandle;
287  *
288  * CryptoKey privateKey;
289  * CryptoKey publicKey;
290  *
291  * int_fast16_t trngResult;
292  * int_fast16_t ecdhResult;
293  *
294  * uint8_t privateKeyingMaterial[32];
295  * uint8_t publicKeyingMaterial[64];
296  *
297  * ECDH_OperationGeneratePublicKey genPubKeyOperation;
298  *
299  * trngHandle = TRNG_open(0, NULL);
300  * if (!trngHandle) {
301  * while(1);
302  * }
303  *
304  * ecdhHandle = ECDH_open(0, NULL);
305  * if (!ecdhHandle) {
306  * while(1);
307  * }
308  *
309  * // Repeatedly generate random numbers until they are in the range [1, n - 1].
310  * // Since the NIST-P256 order is so close to 2^256, the probability of needing
311  * // to generate more than one random number is incredibly low but not non-zero.
312  * do {
313  *
314  * CryptoKeyPlaintext_initBlankKey(&privateKey, privateKeyingMaterial, ECCParams_NISTP256.length);
315  * CryptoKeyPlaintext_initBlankKey(&publicKey, publicKeyingMaterial, 2 * ECCParams_NISTP256.length);
316  *
317  * trngResult = TRNG_generateKey(trngHandle, &privateKey);
318  *
319  * if (trngResult != TRNG_STATUS_SUCCESS) {
320  * while(1);
321  * }
322  *
323  * ECDH_OperationGeneratePublicKey_init(&genPubKeyOperation);
324  * genPubKeyOperation.curve = &ECCParams_NISTP256;
325  * genPubKeyOperation.myPrivateKey = &privateKey;
326  * genPubKeyOperation.myPublicKey = &publicKey;
327  *
328  * ecdhResult = ECDH_generatePublicKey(ecdhHandle, &genPubKeyOperation);
329  *
330  * } while(ecdhResult == ECDH_STATUS_PRIVATE_KEY_LARGER_EQUAL_ORDER || ecdhResult == ECDH_STATUS_PRIVATE_KEY_ZERO);
331  *
332  * TRNG_close(trngHandle);
333  * ECDH_close(ecdhHandle);
334  *
335  * @endcode
336  *
337  * ### Generate random bytes to a user provided buffer #
338  *
339  * @code
340  *
341  * #include <ti/drivers/TRNG.h>
342  *
343  * #define RANDOM_BYTES_SIZE 16
344  *
345  * TRNG_Handle handle;
346  * int_fast16_t result;
347  *
348  * uint8_t randomBytesArray[RANDOM_BYTES_SIZE] = {0};
349  *
350  * handle = TRNG_open(0, NULL);
351  *
352  * if (!handle) {
353  * // Handle error
354  * while(1);
355  * }
356  *
357  * result = TRNG_getRandomBytes(handle, randomBytesArray, RANDOM_BYTES_SIZE);
358  *
359  * if (result != TRNG_STATUS_SUCCESS) {
360  * // Handle error
361  * while(1);
362  * }
363  *
364  * TRNG_close(handle);
365  *
366  * @endcode
367  */
368 
369 #ifndef ti_drivers_TRNG__include
370 #define ti_drivers_TRNG__include
371 
372 #include <stdbool.h>
373 #include <stddef.h>
374 #include <stdint.h>
375 
377 
378 #ifdef __cplusplus
379 extern "C" {
380 #endif
381 
394 #define TRNG_STATUS_RESERVED (-32)
395 
402 #define TRNG_STATUS_SUCCESS (0)
403 
410 #define TRNG_STATUS_ERROR (-1)
411 
420 #define TRNG_STATUS_RESOURCE_UNAVAILABLE (-2)
421 
427 #define TRNG_STATUS_INVALID_INPUTS (-3)
428 
432 #define TRNG_STATUS_CANCELED (-4)
433 
440 #define TRNG_STATUS_KEYSTORE_ERROR (-5)
441 
453 typedef struct
454 {
456  void *object;
457 
459  void const *hwAttrs;
460 } TRNG_Config;
461 
466 
488 typedef enum
489 {
505 
519 typedef void (*TRNG_CryptoKeyCallbackFxn)(TRNG_Handle handle, int_fast16_t returnValue, CryptoKey *entropy);
520 
534 typedef void (*TRNG_RandomBytesCallbackFxn)(TRNG_Handle handle,
535  int_fast16_t returnValue,
536  uint8_t *randomBytes,
537  size_t randomBytesSize);
538 
546 
559 typedef struct
560 {
568  uint32_t timeout;
571  void *custom;
572 } TRNG_Params;
573 
579 extern const TRNG_Params TRNG_defaultParams;
580 
589 void TRNG_init(void);
590 
604 void TRNG_Params_init(TRNG_Params *params);
605 
623 TRNG_Handle TRNG_open(uint_least8_t index, TRNG_Params *params);
624 
634 void TRNG_close(TRNG_Handle handle);
635 
664 int_fast16_t TRNG_generateEntropy(TRNG_Handle handle, CryptoKey *entropy);
665 
698 int_fast16_t TRNG_generateKey(TRNG_Handle handle, CryptoKey *entropy);
699 
731 int_fast16_t TRNG_getRandomBytes(TRNG_Handle handle, void *randomBytes, size_t randomBytesSize);
732 
756 TRNG_Handle TRNG_construct(TRNG_Config *config, const TRNG_Params *params);
757 
770 int_fast16_t TRNG_cancelOperation(TRNG_Handle handle);
771 
772 #ifdef __cplusplus
773 }
774 #endif
775 
776 #endif /* ti_drivers_TRNG__include */
TRNG_Handle TRNG_open(uint_least8_t index, TRNG_Params *params)
This function opens a given TRNG peripheral.
The CryptoKey type is an opaque representation of a cryptographic key.
TRNG Parameters.
Definition: TRNG.h:559
TRNG_ReturnBehavior returnBehavior
Definition: TRNG.h:561
uint32_t timeout
Definition: TRNG.h:568
int_fast16_t TRNG_generateEntropy(TRNG_Handle handle, CryptoKey *entropy)
Generate random bytes and output to the given CryptoKey object.
int_fast16_t TRNG_cancelOperation(TRNG_Handle handle)
Aborts an ongoing TRNG operation and clears internal buffers.
TRNG Global configuration.
Definition: TRNG.h:453
CryptoKey datastructure.
Definition: CryptoKey.h:211
TRNG_Config * TRNG_Handle
A handle that is returned from a TRNG_open() call.
Definition: TRNG.h:465
Definition: TRNG.h:496
TRNG_CryptoKeyCallbackFxn cryptoKeyCallbackFxn
Definition: TRNG.h:562
void TRNG_close(TRNG_Handle handle)
Function to close a TRNG peripheral specified by the TRNG handle.
TRNG_RandomBytesCallbackFxn randomBytesCallbackFxn
Definition: TRNG.h:565
void(* TRNG_CryptoKeyCallbackFxn)(TRNG_Handle handle, int_fast16_t returnValue, CryptoKey *entropy)
The definition of a callback function used by the TRNG driver when TRNG_generateKey() is called with ...
Definition: TRNG.h:519
TRNG_ReturnBehavior
The way in which TRNG function calls return after generating the requested entropy.
Definition: TRNG.h:488
const TRNG_Params TRNG_defaultParams
Default TRNG_Params structure.
int_fast16_t TRNG_getRandomBytes(TRNG_Handle handle, void *randomBytes, size_t randomBytesSize)
Generate random bytes and output to the given array.
void TRNG_Params_init(TRNG_Params *params)
Function to initialize the TRNG_Params struct to its defaults.
TRNG_CryptoKeyCallbackFxn TRNG_CallbackFxn
The definition of a callback function used by the TRNG driver when used in TRNG_RETURN_BEHAVIOR_CALLB...
Definition: TRNG.h:545
void TRNG_init(void)
This function initializes the TRNG module.
int_fast16_t TRNG_generateKey(TRNG_Handle handle, CryptoKey *entropy)
Generate random bytes and output to the given CryptoKey object.
Definition: TRNG.h:500
TRNG_Handle TRNG_construct(TRNG_Config *config, const TRNG_Params *params)
Constructs a new TRNG object.
void const * hwAttrs
Definition: TRNG.h:459
Definition: TRNG.h:490
void * object
Definition: TRNG.h:456
void * custom
Definition: TRNG.h:571
void(* TRNG_RandomBytesCallbackFxn)(TRNG_Handle handle, int_fast16_t returnValue, uint8_t *randomBytes, size_t randomBytesSize)
The definition of a callback function used by the TRNG driver when TRNG_getRandomBytes() is called wi...
Definition: TRNG.h:534
© Copyright 1995-2025, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale