TRNG.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2019, 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 numbers of variable
40  * lengths from a source of entropy. The output is suitable for applications
41  * requiring cryptographically random numbers 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  * - Initialize a blank CryptoKey. These opaque datastructures are representations
55  * of keying material and its storage. Depending on how the keying material
56  * is stored (RAM or flash, key store, key blob), the CryptoKey must be
57  * initialized differently. The TRNG API can handle all types of CryptoKey.
58  * However, not all device-specific implementions support all types of CryptoKey.
59  * Devices without a key store will not support CryptoKeys with keying material
60  * stored in a key store for example.
61  * All devices support plaintext CryptoKeys.
62  *
63  * ## TRNG operations #
64  *
65  * TRNG_generateEntropy() provides the most basic functionality. Use it to
66  * generate random numbers of a specified width without further restrictions.
67  * An example use-case would be generating a symmetric key for AES encryption
68  * and / or authentication.
69  *
70  * To generate an ECC private key, you should use rejection sampling to ensure
71  * that the keying material is in the interval [1, n - 1]. The ECDH public key
72  * genreation APIs will reject private keys that are outside of this interval.
73  * This information may be used to generate keying material until a suitable
74  * key is generated. For most curves, it is improbable to generate a random number
75  * outside of this interval because n is a large number close to the maximum
76  * number that would fit in the k-byte keying material array. An example
77  * of how to do this is given below.
78  *
79  * ## After the TRNG operation completes #
80  *
81  * After the TRNG operation completes, the application should either start another operation
82  * or close the driver by calling TRNG_close().
83  *
84  * @anchor ti_drivers_TRNG_Synopsis
85  * ## Synopsis
86  * @anchor ti_drivers_TRNG_Synopsis_Code
87  * @code
88  * // Import TRNG Driver definitions
89  * #include <ti/drivers/TRNG.h>
90  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
91  *
92  * // Define name for TRNG channel index
93  * #define TRNG_INSTANCE 0
94  *
95  * #define KEY_LENGTH_BYTES 16
96  *
97  * TRNG_init();
98  *
99  * handle = TRNG_open(TRNG_INSTANCE, NULL);
100  *
101  * CryptoKeyPlaintext_initBlankKey(&entropyKey, entropyBuffer, KEY_LENGTH_BYTES);
102  *
103  * result = TRNG_generateEntropy(handle, &entropyKey);
104  *
105  * TRNG_close(handle);
106  *
107  * @endcode
108  *
109  * @anchor ti_drivers_TRNG_Examples
110  * ## Examples
111  *
112  * ### Generate symmetric encryption key #
113  * @code
114  *
115  * #include <ti/drivers/TRNG.h>
116  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
117  *
118  * #define KEY_LENGTH_BYTES 16
119  *
120  * TRNG_Handle handle;
121  * int_fast16_t result;
122  *
123  * CryptoKey entropyKey;
124  * uint8_t entropyBuffer[KEY_LENGTH_BYTES];
125  *
126  * handle = TRNG_open(0, NULL);
127  *
128  * if (!handle) {
129  * // Handle error
130  * while(1);
131  * }
132  *
133  * CryptoKeyPlaintext_initBlankKey(&entropyKey, entropyBuffer, KEY_LENGTH_BYTES);
134  *
135  * result = TRNG_generateEntropy(handle, &entropyKey);
136  *
137  * if (result != TRNG_STATUS_SUCCESS) {
138  * // Handle error
139  * while(1);
140  * }
141  *
142  * TRNG_close(handle);
143  *
144  * @endcode
145  *
146  * ### Generate ECC private and public key using rejection sampling #
147  * @code
148  *
149  * #include <ti/drivers/TRNG.h>
150  * #include <ti/drivers/ECDH.h>
151  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
152  * #include <ti/drivers/cryptoutils/ecc/ECCParams.h>
153  *
154  * TRNG_Handle trngHandle;
155  * ECDH_Handle ecdhHandle;
156  *
157  * CryptoKey privateKey;
158  * CryptoKey publicKey;
159  *
160  * int_fast16_t trngResult;
161  * int_fast16_t ecdhResult;
162  *
163  * uint8_t privateKeyingMaterial[32];
164  * uint8_t publicKeyingMaterial[64];
165  *
166  * ECDH_OperationGeneratePublicKey genPubKeyOperation;
167  *
168  * trngHandle = TRNG_open(0, NULL);
169  * if (!trngHandle) {
170  * while(1);
171  * }
172  *
173  * ecdhHandle = ECDH_open(0, NULL);
174  * if (!ecdhHandle) {
175  * while(1);
176  * }
177  *
178  * // Repeatedly generate random numbers until they are in the range [1, n - 1].
179  * // Since the NIST-P256 order is so close to 2^256, the probability of needing
180  * // to generate more than one random number is incredibly low but not non-zero.
181  * do {
182  *
183  * CryptoKeyPlaintext_initBlankKey(&privateKey, privateKeyingMaterial, ECCParams_NISTP256.length);
184  * CryptoKeyPlaintext_initBlankKey(&publicKey, publicKeyingMaterial, 2 * ECCParams_NISTP256.length);
185  *
186  * trngResult = TRNG_generateEntropy(trngHandle, &privateKey);
187  *
188  * if (trngResult != TRNG_STATUS_SUCCESS) {
189  * while(1);
190  * }
191  *
192  * ECDH_OperationGeneratePublicKey_init(&genPubKeyOperation);
193  * genPubKeyOperation.curve = &ECCParams_NISTP256;
194  * genPubKeyOperation.myPrivateKey = &privateKey;
195  * genPubKeyOperation.myPublicKey = &publicKey;
196  *
197  * ecdhResult = ECDH_generatePublicKey(ecdhHandle, &genPubKeyOperation);
198  *
199  * } while(ecdhResult == ECDH_STATUS_PRIVATE_KEY_LARGER_EQUAL_ORDER || ecdhResult == ECDH_STATUS_PRIVATE_KEY_ZERO);
200  *
201  * TRNG_close(trngHandle);
202  * ECDH_close(ecdhHandle);
203  *
204  * @endcode
205  */
206 
207 #ifndef ti_drivers_TRNG__include
208 #define ti_drivers_TRNG__include
209 
210 #include <stdbool.h>
211 #include <stddef.h>
212 #include <stdint.h>
213 
215 
216 #ifdef __cplusplus
217 extern "C" {
218 #endif
219 
232 #define TRNG_STATUS_RESERVED (-32)
233 
240 #define TRNG_STATUS_SUCCESS (0)
241 
248 #define TRNG_STATUS_ERROR (-1)
249 
258 #define TRNG_STATUS_RESOURCE_UNAVAILABLE (-2)
259 
271 typedef struct {
273  void *object;
274 
276  void const *hwAttrs;
277 } TRNG_Config;
278 
283 
305 typedef enum {
321 
333 typedef void (*TRNG_CallbackFxn) (TRNG_Handle handle,
334  int_fast16_t returnValue,
335  CryptoKey *entropy);
336 
345 typedef struct {
348  uint32_t timeout;
351  void *custom;
354 } TRNG_Params;
355 
361 extern const TRNG_Params TRNG_defaultParams;
362 
371 void TRNG_init(void);
372 
386 
404 TRNG_Handle TRNG_open(uint_least8_t index, TRNG_Params *params);
405 
415 void TRNG_close(TRNG_Handle handle);
416 
436 int_fast16_t TRNG_generateEntropy(TRNG_Handle handle, CryptoKey *entropy);
437 
461 TRNG_Handle TRNG_construct(TRNG_Config *config, const TRNG_Params *params);
462 
463 
464 #ifdef __cplusplus
465 }
466 #endif
467 
468 #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:345
TRNG_ReturnBehavior returnBehavior
Definition: TRNG.h:346
uint32_t timeout
Definition: TRNG.h:348
int_fast16_t TRNG_generateEntropy(TRNG_Handle handle, CryptoKey *entropy)
Generate a random number.
TRNG Global configuration.
Definition: TRNG.h:271
TRNG_CallbackFxn callbackFxn
Definition: TRNG.h:347
CryptoKey datastructure.
Definition: CryptoKey.h:209
TRNG_Config * TRNG_Handle
A handle that is returned from a TRNG_open() call.
Definition: TRNG.h:282
Definition: TRNG.h:312
void TRNG_close(TRNG_Handle handle)
Function to close a TRNG peripheral specified by the TRNG handle.
TRNG_ReturnBehavior
The way in which TRNG function calls return after generating the requested entropy.
Definition: TRNG.h:305
const TRNG_Params TRNG_defaultParams
Default TRNG_Params structure.
void TRNG_Params_init(TRNG_Params *params)
Function to initialize the TRNG_Params struct to its defaults.
void TRNG_init(void)
This function initializes the TRNG module.
Definition: TRNG.h:316
void(* TRNG_CallbackFxn)(TRNG_Handle handle, int_fast16_t returnValue, CryptoKey *entropy)
The definition of a callback function used by the TRNG driver when used in TRNG_RETURN_BEHAVIOR_CALLB...
Definition: TRNG.h:333
TRNG_Handle TRNG_construct(TRNG_Config *config, const TRNG_Params *params)
Constructs a new TRNG object.
void const * hwAttrs
Definition: TRNG.h:276
Definition: TRNG.h:306
void * object
Definition: TRNG.h:273
void * custom
Definition: TRNG.h:351
© Copyright 1995-2020, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale