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  * @warning This is a beta API. It may change in future releases.
38  *
39  * @anchor ti_drivers_TRNG_Overview
40  * # Overview #
41  * The True Random Number Generator (TRNG) module generates numbers of variable
42  * lengths from a source of entropy. The output is suitable for applications
43  * requiring cryptographically random numbers such as keying material for
44  * private or symmetric keys.
45  *
46  * @anchor ti_drivers_TRNG_Usage
47  * # Usage #
48  *
49  * ## Before starting a TRNG operation #
50  *
51  * Before starting a TRNG operation, the application must do the following:
52  * - Call TRNG_init() to initialize the driver.
53  * - Call TRNG_Params_init() to initialize the TRNG_Params to default values.
54  * - Modify the TRNG_Params as desired.
55  * - Call TRNG_open() to open an instance of the driver.
56  * - Initialize a blank CryptoKey. These opaque datastructures are representations
57  * of keying material and its storage. Depending on how the keying material
58  * is stored (RAM or flash, key store, key blob), the CryptoKey must be
59  * initialized differently. The TRNG API can handle all types of CryptoKey.
60  * However, not all device-specific implementions support all types of CryptoKey.
61  * Devices without a key store will not support CryptoKeys with keying material
62  * stored in a key store for example.
63  * All devices support plaintext CryptoKeys.
64  *
65  * ## TRNG operations #
66  *
67  * TRNG_generateEntropy() provides the most basic functionality. Use it to
68  * generate random numbers of a specified width without further restrictions.
69  * An example use-case would be generating a symmetric key for AES encryption
70  * and / or authentication.
71  *
72  * To generate an ECC private key, you should use rejection sampling to ensure
73  * that the keying material is in the interval [1, n - 1]. The ECDH public key
74  * genreation APIs will reject private keys that are outside of this interval.
75  * This information may be used to generate keying material until a suitable
76  * key is generated. For most curves, it is improbable to generate a random number
77  * outside of this interval because n is a large number close to the maximum
78  * number that would fit in the k-byte keying material array. An example
79  * of how to do this is given below.
80  *
81  * ## After the TRNG operation completes #
82  *
83  * After the TRNG operation completes, the application should either start another operation
84  * or close the driver by calling TRNG_close().
85  *
86  * @anchor ti_drivers_TRNG_Synopsis
87  * ## Synopsis
88  * @anchor ti_drivers_TRNG_Synopsis_Code
89  * @code
90  * // Import TRNG Driver definitions
91  * #include <ti/drivers/TRNG.h>
92  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
93  *
94  * // Define name for TRNG channel index
95  * #define TRNG_INSTANCE 0
96  *
97  * #define KEY_LENGTH_BYTES 16
98  *
99  * TRNG_init();
100  *
101  * handle = TRNG_open(TRNG_INSTANCE, NULL);
102  *
103  * CryptoKeyPlaintext_initBlankKey(&entropyKey, entropyBuffer, KEY_LENGTH_BYTES);
104  *
105  * result = TRNG_generateEntropy(handle, &entropyKey);
106  *
107  * TRNG_close(handle);
108  *
109  * @endcode
110  *
111  * @anchor ti_drivers_TRNG_Examples
112  * ## Examples
113  *
114  * ### Generate symmetric encryption key #
115  * @code
116  *
117  * #include <ti/drivers/TRNG.h>
118  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
119  *
120  * #define KEY_LENGTH_BYTES 16
121  *
122  * TRNG_Handle handle;
123  * int_fast16_t result;
124  *
125  * CryptoKey entropyKey;
126  * uint8_t entropyBuffer[KEY_LENGTH_BYTES];
127  *
128  * handle = TRNG_open(0, NULL);
129  *
130  * if (!handle) {
131  * // Handle error
132  * while(1);
133  * }
134  *
135  * CryptoKeyPlaintext_initBlankKey(&entropyKey, entropyBuffer, KEY_LENGTH_BYTES);
136  *
137  * result = TRNG_generateEntropy(handle, &entropyKey);
138  *
139  * if (result != TRNG_STATUS_SUCCESS) {
140  * // Handle error
141  * while(1);
142  * }
143  *
144  * TRNG_close(handle);
145  *
146  * @endcode
147  *
148  * ### Generate ECC private and public key using rejection sampling #
149  * @code
150  *
151  * #include <ti/drivers/TRNG.h>
152  * #include <ti/drivers/ECDH.h>
153  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
154  * #include <ti/drivers/cryptoutils/ecc/ECCParams.h>
155  *
156  * TRNG_Handle trngHandle;
157  * ECDH_Handle ecdhHandle;
158  *
159  * CryptoKey privateKey;
160  * CryptoKey publicKey;
161  *
162  * int_fast16_t trngResult;
163  * int_fast16_t ecdhResult;
164  *
165  * uint8_t privateKeyingMaterial[32];
166  * uint8_t publicKeyingMaterial[64];
167  *
168  * ECDH_OperationGeneratePublicKey genPubKeyOperation;
169  *
170  * trngHandle = TRNG_open(0, NULL);
171  * if (!trngHandle) {
172  * while(1);
173  * }
174  *
175  * ecdhHandle = ECDH_open(0, NULL);
176  * if (!ecdhHandle) {
177  * while(1);
178  * }
179  *
180  * // Repeatedly generate random numbers until they are in the range [1, n - 1].
181  * // Since the NIST-P256 order is so close to 2^256, the probability of needing
182  * // to generate more than one random number is incredibly low but not non-zero.
183  * do {
184  *
185  * CryptoKeyPlaintext_initBlankKey(&privateKey, privateKeyingMaterial, ECCParams_NISTP256.length);
186  * CryptoKeyPlaintext_initBlankKey(&publicKey, publicKeyingMaterial, 2 * ECCParams_NISTP256.length);
187  *
188  * trngResult = TRNG_generateEntropy(trngHandle, &privateKey);
189  *
190  * if (trngResult != TRNG_STATUS_SUCCESS) {
191  * while(1);
192  * }
193  *
194  * ECDH_OperationGeneratePublicKey_init(&genPubKeyOperation);
195  * genPubKeyOperation.curve = &ECCParams_NISTP256;
196  * genPubKeyOperation.myPrivateKey = &privateKey;
197  * genPubKeyOperation.myPublicKey = &publicKey;
198  *
199  * ecdhResult = ECDH_generatePublicKey(ecdhHandle, &genPubKeyOperation);
200  *
201  * } while(ecdhResult == ECDH_STATUS_PRIVATE_KEY_LARGER_EQUAL_ORDER || ecdhResult == ECDH_STATUS_PRIVATE_KEY_ZERO);
202  *
203  * TRNG_close(trngHandle);
204  * ECDH_close(ecdhHandle);
205  *
206  * @endcode
207  */
208 
209 #ifndef ti_drivers_TRNG__include
210 #define ti_drivers_TRNG__include
211 
212 #include <stdbool.h>
213 #include <stddef.h>
214 #include <stdint.h>
215 
217 
218 #ifdef __cplusplus
219 extern "C" {
220 #endif
221 
234 #define TRNG_STATUS_RESERVED (-32)
235 
242 #define TRNG_STATUS_SUCCESS (0)
243 
250 #define TRNG_STATUS_ERROR (-1)
251 
260 #define TRNG_STATUS_RESOURCE_UNAVAILABLE (-2)
261 
273 typedef struct {
275  void *object;
276 
278  void const *hwAttrs;
279 } TRNG_Config;
280 
285 
307 typedef enum {
323 
335 typedef void (*TRNG_CallbackFxn) (TRNG_Handle handle,
336  int_fast16_t returnValue,
337  CryptoKey *entropy);
338 
347 typedef struct {
350  uint32_t timeout;
353  void *custom;
356 } TRNG_Params;
357 
363 extern const TRNG_Params TRNG_defaultParams;
364 
373 void TRNG_init(void);
374 
387 void TRNG_Params_init(TRNG_Params *params);
388 
406 TRNG_Handle TRNG_open(uint_least8_t index, TRNG_Params *params);
407 
417 void TRNG_close(TRNG_Handle handle);
418 
438 int_fast16_t TRNG_generateEntropy(TRNG_Handle handle, CryptoKey *entropy);
439 
463 TRNG_Handle TRNG_construct(TRNG_Config *config, const TRNG_Params *params);
464 
465 
466 #ifdef __cplusplus
467 }
468 #endif
469 
470 #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:347
TRNG_ReturnBehavior returnBehavior
Definition: TRNG.h:348
uint32_t timeout
Definition: TRNG.h:350
int_fast16_t TRNG_generateEntropy(TRNG_Handle handle, CryptoKey *entropy)
Generate a random number.
TRNG Global configuration.
Definition: TRNG.h:273
TRNG_CallbackFxn callbackFxn
Definition: TRNG.h:349
CryptoKey datastructure.
Definition: CryptoKey.h:209
TRNG_Config * TRNG_Handle
A handle that is returned from a TRNG_open() call.
Definition: TRNG.h:284
Definition: TRNG.h:314
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:307
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:318
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:335
TRNG_Handle TRNG_construct(TRNG_Config *config, const TRNG_Params *params)
Constructs a new TRNG object.
void const * hwAttrs
Definition: TRNG.h:278
Definition: TRNG.h:308
void * object
Definition: TRNG.h:275
void * custom
Definition: TRNG.h:353
© Copyright 1995-2019, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale