TRNG.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2021, 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  * To generate an ECC private key, you should use rejection sampling to ensure
75  * that the keying material is in the interval [1, n - 1]. The ECDH public key
76  * generation APIs will reject private keys that are outside of this interval.
77  * This information may be used to generate keying material until a suitable
78  * key is generated. For most curves, it is improbable to generate a random number
79  * outside of this interval because n is a large number close to the maximum
80  * number that would fit in the k-byte keying material array. An example
81  * of how to do this is given below.
82  *
83  * ## After the TRNG operation completes #
84  *
85  * After the TRNG operation completes, the application should either start another operation
86  * or close the driver by calling TRNG_close().
87  *
88  * @anchor ti_drivers_TRNG_Synopsis
89  * ## Synopsis
90  * @anchor ti_drivers_TRNG_Synopsis_Code
91  * @code
92  * // Import TRNG Driver definitions
93  * #include <ti/drivers/TRNG.h>
94  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
95  *
96  * // Define name for TRNG channel index
97  * #define TRNG_INSTANCE 0
98  *
99  * #define KEY_LENGTH_BYTES 16
100  *
101  * TRNG_init();
102  *
103  * handle = TRNG_open(TRNG_INSTANCE, NULL);
104  *
105  * CryptoKeyPlaintext_initBlankKey(&entropyKey, entropyBuffer, KEY_LENGTH_BYTES);
106  *
107  * result = TRNG_generateKey(handle, &entropyKey);
108  *
109  * TRNG_close(handle);
110  *
111  * @endcode
112  *
113  * @anchor ti_drivers_TRNG_Examples
114  * ## Examples
115  *
116  * ### Generate symmetric encryption key #
117  *
118  * @code
119  *
120  * #include <ti/drivers/TRNG.h>
121  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
122  *
123  * #define KEY_LENGTH_BYTES 16
124  *
125  * TRNG_Handle handle;
126  * int_fast16_t result;
127  *
128  * CryptoKey entropyKey;
129  * uint8_t entropyBuffer[KEY_LENGTH_BYTES] = {0};
130  *
131  * handle = TRNG_open(0, NULL);
132  *
133  * if (!handle) {
134  * // Handle error
135  * while(1);
136  * }
137  *
138  * CryptoKeyPlaintext_initBlankKey(&entropyKey, entropyBuffer, KEY_LENGTH_BYTES);
139  *
140  * result = TRNG_generateKey(handle, &entropyKey);
141  *
142  * if (result != TRNG_STATUS_SUCCESS) {
143  * // Handle error
144  * while(1);
145  * }
146  *
147  * TRNG_close(handle);
148  *
149  * @endcode
150  *
151  * ### Generate ECC private and public key using rejection sampling #
152  *
153  * @code
154  *
155  * #include <ti/drivers/TRNG.h>
156  * #include <ti/drivers/ECDH.h>
157  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
158  * #include <ti/drivers/cryptoutils/ecc/ECCParams.h>
159  *
160  * TRNG_Handle trngHandle;
161  * ECDH_Handle ecdhHandle;
162  *
163  * CryptoKey privateKey;
164  * CryptoKey publicKey;
165  *
166  * int_fast16_t trngResult;
167  * int_fast16_t ecdhResult;
168  *
169  * uint8_t privateKeyingMaterial[32];
170  * uint8_t publicKeyingMaterial[64];
171  *
172  * ECDH_OperationGeneratePublicKey genPubKeyOperation;
173  *
174  * trngHandle = TRNG_open(0, NULL);
175  * if (!trngHandle) {
176  * while(1);
177  * }
178  *
179  * ecdhHandle = ECDH_open(0, NULL);
180  * if (!ecdhHandle) {
181  * while(1);
182  * }
183  *
184  * // Repeatedly generate random numbers until they are in the range [1, n - 1].
185  * // Since the NIST-P256 order is so close to 2^256, the probability of needing
186  * // to generate more than one random number is incredibly low but not non-zero.
187  * do {
188  *
189  * CryptoKeyPlaintext_initBlankKey(&privateKey, privateKeyingMaterial, ECCParams_NISTP256.length);
190  * CryptoKeyPlaintext_initBlankKey(&publicKey, publicKeyingMaterial, 2 * ECCParams_NISTP256.length);
191  *
192  * trngResult = TRNG_generateKey(trngHandle, &privateKey);
193  *
194  * if (trngResult != TRNG_STATUS_SUCCESS) {
195  * while(1);
196  * }
197  *
198  * ECDH_OperationGeneratePublicKey_init(&genPubKeyOperation);
199  * genPubKeyOperation.curve = &ECCParams_NISTP256;
200  * genPubKeyOperation.myPrivateKey = &privateKey;
201  * genPubKeyOperation.myPublicKey = &publicKey;
202  *
203  * ecdhResult = ECDH_generatePublicKey(ecdhHandle, &genPubKeyOperation);
204  *
205  * } while(ecdhResult == ECDH_STATUS_PRIVATE_KEY_LARGER_EQUAL_ORDER || ecdhResult == ECDH_STATUS_PRIVATE_KEY_ZERO);
206  *
207  * TRNG_close(trngHandle);
208  * ECDH_close(ecdhHandle);
209  *
210  * @endcode
211  *
212  * ### Generate random bytes to a user provided buffer #
213  *
214  * @code
215  *
216  * #include <ti/drivers/TRNG.h>
217  *
218  * #define RANDOM_BYTES_SIZE 16
219  *
220  * TRNG_Handle handle;
221  * int_fast16_t result;
222  *
223  * uint8_t randomBytesArray[RANDOM_BYTES_SIZE] = {0};
224  *
225  * handle = TRNG_open(0, NULL);
226  *
227  * if (!handle) {
228  * // Handle error
229  * while(1);
230  * }
231  *
232  * result = TRNG_getRandomBytes(handle, randomBytesArray, RANDOM_BYTES_SIZE);
233  *
234  * if (result != TRNG_STATUS_SUCCESS) {
235  * // Handle error
236  * while(1);
237  * }
238  *
239  * TRNG_close(handle);
240  *
241  * @endcode
242  */
243 
244 #ifndef ti_drivers_TRNG__include
245 #define ti_drivers_TRNG__include
246 
247 #include <stdbool.h>
248 #include <stddef.h>
249 #include <stdint.h>
250 
252 
253 #ifdef __cplusplus
254 extern "C" {
255 #endif
256 
269 #define TRNG_STATUS_RESERVED (-32)
270 
277 #define TRNG_STATUS_SUCCESS (0)
278 
285 #define TRNG_STATUS_ERROR (-1)
286 
295 #define TRNG_STATUS_RESOURCE_UNAVAILABLE (-2)
296 
302 #define TRNG_STATUS_INVALID_INPUTS (-3)
303 
307 #define TRNG_STATUS_CANCELED (-4)
308 
320 typedef struct
321 {
323  void *object;
324 
326  void const *hwAttrs;
327 } TRNG_Config;
328 
333 
355 typedef enum
356 {
372 
386 typedef void (*TRNG_CryptoKeyCallbackFxn)(TRNG_Handle handle, int_fast16_t returnValue, CryptoKey *entropy);
387 
401 typedef void (*TRNG_RandomBytesCallbackFxn)(TRNG_Handle handle,
402  int_fast16_t returnValue,
403  uint8_t *randomBytes,
404  size_t randomBytesSize);
405 
413 
426 typedef struct
427 {
435  uint32_t timeout;
438  void *custom;
439 } TRNG_Params;
440 
446 extern const TRNG_Params TRNG_defaultParams;
447 
456 void TRNG_init(void);
457 
472 
490 TRNG_Handle TRNG_open(uint_least8_t index, TRNG_Params *params);
491 
501 void TRNG_close(TRNG_Handle handle);
502 
527 int_fast16_t TRNG_generateEntropy(TRNG_Handle handle, CryptoKey *entropy);
528 
556 int_fast16_t TRNG_generateKey(TRNG_Handle handle, CryptoKey *entropy);
557 
584 int_fast16_t TRNG_getRandomBytes(TRNG_Handle handle, void *randomBytes, size_t randomBytesSize);
585 
609 TRNG_Handle TRNG_construct(TRNG_Config *config, const TRNG_Params *params);
610 
623 int_fast16_t TRNG_cancelOperation(TRNG_Handle handle);
624 
625 #ifdef __cplusplus
626 }
627 #endif
628 
629 #endif /* ti_drivers_TRNG__include */
TRNG_Handle TRNG_open(uint_least8_t index, TRNG_Params *params)
This function opens a given TRNG peripheral.
ADC_Params params
Definition: Driver_Init.h:11
The CryptoKey type is an opaque representation of a cryptographic key.
TRNG Parameters.
Definition: TRNG.h:426
TRNG_ReturnBehavior returnBehavior
Definition: TRNG.h:428
uint32_t timeout
Definition: TRNG.h:435
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:320
CryptoKey datastructure.
Definition: CryptoKey.h:192
TRNG_Config * TRNG_Handle
A handle that is returned from a TRNG_open() call.
Definition: TRNG.h:332
Definition: TRNG.h:363
TRNG_CryptoKeyCallbackFxn cryptoKeyCallbackFxn
Definition: TRNG.h:429
void TRNG_close(TRNG_Handle handle)
Function to close a TRNG peripheral specified by the TRNG handle.
TRNG_RandomBytesCallbackFxn randomBytesCallbackFxn
Definition: TRNG.h:432
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:386
TRNG_ReturnBehavior
The way in which TRNG function calls return after generating the requested entropy.
Definition: TRNG.h:355
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:412
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:367
TRNG_Handle TRNG_construct(TRNG_Config *config, const TRNG_Params *params)
Constructs a new TRNG object.
void const * hwAttrs
Definition: TRNG.h:326
Definition: TRNG.h:357
void * object
Definition: TRNG.h:323
void * custom
Definition: TRNG.h:438
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:401
© Copyright 1995-2022, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale