AESCTRDRBG.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-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 AESCTRDRBG.h
34  *
35  * @brief AESCTRDRBG driver header
36  *
37  * @anchor ti_drivers_AESCTRDRBG_Overview
38  * <h3> Overview </h3>
39  * AESCTRDRBG is a cryptographically secure deterministic random bit generator
40  * that is used to efficiently generate random numbers for use in keying material
41  * or other security related purposes. It is based on the AES block cipher
42  * operating in Counter (CTR) mode and is defined by NIST SP 800-90A.
43  *
44  * AESCTRDRBG derives a sequence of pseudo-random numbers based on an initial
45  * secret seed and additional, non-secret personalization data provided during
46  * instantiation. A sequence of random bits generated by AESCTRDRBG will have
47  * an equivalent entropy content of MIN(sequenceLength, security strength).
48  * The security strength is based on the seed length and the AES key length used
49  * in the AESCTRDRBG instance.
50  *
51  * | | AES-128 | AES-192 | AES-256 |
52  * |---------------------------------------|---------|---------|---------|
53  * | Security Strength (bits) | 128 | 192 | 256 |
54  * | Seed Length (bits) | 192 | 320 | 384 |
55  * | Personalization String Length (bits) | <= 192 | <= 320 | <= 384 |
56  * | Max Requests Between Reseeds | 2^48 | 2^48 | 2^48 |
57  * | Max Request Length (bits) | 2^19 | 2^19 | 2^19 |
58  *
59  * <h3> Security Strength </h3>
60  * The seed must be sourced from a cryptographically secure source such as
61  * a True Random Number Generator and contain seed length bits of entropy.
62  * Since the seed length is always larger than the security strength for
63  * any one AES key length, the output of one AESCTRDRBG instance may not
64  * be used to seed another instance of the same or higher security strength.
65  *
66  * <h3> Reseeding </h3>
67  * Because of the way AES CTR operates, there are a limited number of output
68  * bitstreams that may be generated before the AESCTRDRBG instance must be
69  * reseeded. The reseeding interval is set by the number of random bit
70  * sequences generated and not by their individual or combined lengths. Each time
71  * random bits are requested of the AESCTRDRBG instance by the application,
72  * the reseed counter is incremented by one regardless of how many bits at a
73  * time are requested. When this counter reaches the configured reseed limit,
74  * the AESCTRDRBG instance will return #AESCTRDRBG_STATUS_RESEED_REQUIRED
75  * until it is reseeded.
76  *
77  * The maximum permitted number of requests between reseeds is 2^48.
78  * The default counter is only 2^32 long for ease of implementation.
79  * A more conservative reseed limit may be configured by the application
80  * for increased security.
81  *
82  * A previously used seed may never be reused to reseed an AESCTRDRBG instance.
83  * The seed used to instantiate or reseed an instance must be generated by
84  * an approved entropy source.
85  *
86  * <h3> Derivation Function </h3>
87  * NIST specifies the the use of an optional derivation function to reduced
88  * entropy and personalization string lengths longer than the seed
89  * length down to the seed length. This feature is not presently supported.
90  *
91  * @anchor ti_drivers_AESCTRDRBG_Usage
92  * <h3> Usage </h3>
93  *
94  * This documentation provides a basic @ref ti_drivers_AESCTRDRBG_Synopsis
95  * "usage summary" and a set of @ref ti_drivers_AESCTRDRBG_Examples "examples"
96  * in the form of commented code fragments. Detailed descriptions of the
97  * APIs are provided in subsequent sections.
98  *
99  * @anchor ti_drivers_AESCTRDRBG_Synopsis
100  * <h3> Synopsis </h3>
101  * @anchor ti_drivers_AESCTRDRBG_Synopsis_Code
102  * @code
103  * #include <ti/drivers/AESCTRDRBG.h>
104  *
105  * AESCTRDRBG_init();
106  *
107  * // Instantiate the AESCTRDRBG instance
108  * AESCTRDRBG_Params_init(&params);
109  * params.keyLength = AESCTRDRBG_AES_KEY_LENGTH_128;
110  * params.reseedInterval = 0xFFFFFFFF;
111  * params.seed = seedBuffer;
112  *
113  * handle = AESCTRDRBG_open(0, &params);
114  *
115  * result = AESCTRDRBG_generateKey(handle, &resultKey);
116  *
117  * reseedResult = AESCTRDRBG_reseed(handle, reseedBuffer, NULL, 0);
118  *
119  * AESCTRDRBG_close(handle);
120  * @endcode
121  *
122  * @anchor ti_drivers_AESCTRDRBG_Examples
123  * <h3> Examples </h3>
124  *
125  * <h4> Instantiating an AESCTRDRBG Instance with TRNG </h4>
126  * @code
127  *
128  * #include <ti/drivers/AESCTRDRBG.h>
129  * #include <ti/drivers/TRNG.h>
130  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
131  *
132  * ...
133  *
134  * AESCTRDRBG_Handle handle;
135  * AESCTRDRBG_Params params;
136  * TRNG_Handle trngHandle;
137  * int_fast16_t result;
138  *
139  * uint8_t seedBuffer[AESCTRDRBG_SEED_LENGTH_AES_128];
140  *
141  * // Open a TRNG driver instance
142  * trngHandle = TRNG_open(0, NULL);
143  * if (trngHandle == NULL) {
144  * // Failed to open TRNG instance
145  * while(1);
146  * }
147  *
148  * // Generate entropy for the seed
149  * result = TRNG_getRandomBytes(trngHandle, seedBuffer, AESCTRDRBG_SEED_LENGTH_AES_128);
150  * if (result != TRNG_STATUS_SUCCESS) {
151  * // Failed to generate entropy
152  * while(1);
153  * }
154  *
155  * TRNG_close(trngHandle);
156  *
157  * // Instantiate the AESCTRDRBG parameters and the driver instance
158  * AESCTRDRBG_Params_init(&params);
159  * params.keyLength = AESCTRDRBG_AES_KEY_LENGTH_128;
160  * params.reseedInterval = 0xFFFFFFFF;
161  * params.seed = seedBuffer;
162  *
163  * handle = AESCTRDRBG_open(0, &params);
164  * if (handle == NULL) {
165  * // Failed to open AESCTRDRBG instance
166  * while(1);
167  * }
168  * @endcode
169  *
170  * <h4> Generating random key with Reseeding </h4>
171  *
172  * @code
173  *
174  * #include <ti/drivers/AESCTRDRBG.h>
175  * #include <ti/drivers/TRNG.h>
176  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
177  *
178  * ...
179  *
180  * #define RANDOM_KEY_LENGTH_BYTES 32
181  *
182  * AESCTRDRBG_Handle handle;
183  * TRNG_Handle trngHandle;
184  * CryptoKey randomKey;
185  * int_fast16_t result;
186  *
187  * uint8_t randomKeyBuffer[RANDOM_KEY_LENGTH_BYTES];
188  *
189  * // Initialise the AESCTRDRBG params and driver instance here
190  * ...
191  *
192  * // Initialize a blank CryptoKey
193  * CryptoKeyPlaintext_initBlankKey(&randomKey, randomKeyBuffer, RANDOM_KEY_LENGTH_BYTES);
194  *
195  * // Generate key-material for the CryptoKey
196  * result = AESCTRDRBG_generateKey(handle, &randomKey);
197  *
198  * // Check return value and reseed if needed. This should happen only after many invocations
199  * // of AESCTRDRBG_generateKey().
200  * if (result == AESCTRDRBG_STATUS_RESEED_REQUIRED) {
201  * TRNG_Handle trngHandle;
202  * int_fast16_t reseedResult;
203  * uint8_t reseedBuffer[AESCTRDRBG_SEED_LENGTH_AES_128];
204  *
205  * reseedResult = TRNG_getRandomBytes(trngHandle, reseedBuffer, AESCTRDRBG_SEED_LENGTH_AES_128);
206  * if (reseedResult != TRNG_STATUS_SUCCESS) {
207  * // Failed to generate entropy
208  * while(1);
209  * }
210  *
211  * TRNG_close(trngHandle);
212  *
213  * // Reseed the DRBG instance
214  * reseedResult = AESCTRDRBG_reseed(handle, reseedBuffer, NULL, 0);
215  * if (reseedResult != AESCTRDRBG_STATUS_SUCCESS) {
216  * // Failed to reseed the DRBG instance
217  * while(1);
218  * }
219  *
220  * // If AESCTRDRBG_STATUS_RESEED_REQUIRED was returned from the previous call to
221  * // AESCTRDRBG_generateKey(), the random key was never generated by that call.
222  * // So the user must invoke that call again (after reseeding) to get a random key.
223  * result = AESCTRDRBG_generateKey(handle, &randomKey);
224  * if (result != AESCTRDRBG_STATUS_SUCCESS) {
225  * // Failed to generate key-material
226  * while(1);
227  * }
228  * }
229  * else if (result != AESCTRDRBG_STATUS_SUCCESS) {
230  * // Failed to generate key-material
231  * while(1);
232  * }
233  *
234  * @endcode
235  *
236  * <h4> Generating random bytes output to an array </h4>
237  *
238  * @code
239  *
240  * #include <ti/drivers/AESCTRDRBG.h>
241  * #include <ti/drivers/TRNG.h>
242  *
243  * ...
244  *
245  * #define RANDOM_BYTES_SIZE 16
246  *
247  * AESCTRDRBG_Handle handle;
248  * TRNG_Handle trngHandle;
249  * int_fast16_t result;
250  * uint8_t randomBytesBuffer[RANDOM_BYTES_SIZE];
251  *
252  * // Initialise the AESCTRDRBG params and driver instance here
253  * ...
254  *
255  * // Get random bytes output to the buffer/array
256  * result = AESCTRDRBG_getRandomBytes(handle, &randomBytesBuffer, RANDOM_BYTES_SIZE);
257  *
258  * // Check and reseed if required. This should happen only after many invocations
259  * // of AESCTRDRBG_getRandomBytes().
260  * if (result == AESCTRDRBG_STATUS_RESEED_REQUIRED) {
261  * // Reseed the DRBG instance using AESCTRDRBG_reseed()
262  * // and invoke AESCTRDRBG_getRandomBytes() similar to the previous example.
263  * }
264  * else if (result != AESCTRDRBG_STATUS_SUCCESS) {
265  * // Failed to generate random bytes
266  * while(1);
267  * }
268  *
269  * @endcode
270  *
271  */
272 
273 #ifndef ti_drivers_AESCTRDRBG__include
274 #define ti_drivers_AESCTRDRBG__include
275 
276 #include <stdbool.h>
277 #include <stddef.h>
278 #include <stdint.h>
279 
280 #include <ti/drivers/AESCTR.h>
281 #include <ti/drivers/AESCommon.h>
283 
284 #ifdef __cplusplus
285 extern "C" {
286 #endif
287 
300 #define AESCTRDRBG_STATUS_RESERVED AES_STATUS_RESERVED
301 
308 #define AESCTRDRBG_STATUS_SUCCESS AES_STATUS_SUCCESS
309 
316 #define AESCTRDRBG_STATUS_ERROR AES_STATUS_ERROR
317 
326 #define AESCTRDRBG_STATUS_RESOURCE_UNAVAILABLE AES_STATUS_RESOURCE_UNAVAILABLE
327 
334 #define AESCTRDRBG_STATUS_RESEED_REQUIRED (AES_STATUS_DRIVER_SPECIFIC_ERROR - 0)
335 
340 #define AESCTRDRBG_STATUS_UNINSTANTIATED (AES_STATUS_DRIVER_SPECIFIC_ERROR - 1)
341 
349 #define AESCTRDRBG_STATUS_UNALIGNED_IO_NOT_SUPPORTED (AES_STATUS_DRIVER_SPECIFIC_ERROR - 2)
350 
354 #define AESCTRDRBG_AES_BLOCK_SIZE_BYTES 16
355 
359 typedef enum
360 {
364 
368 typedef enum
369 {
373 
394 typedef enum
395 {
407 
420 
424 typedef AESCTRDRBG_Config *AESCTRDRBG_Handle;
425 
434 typedef struct
435 {
439  uint32_t reseedInterval;
443  const void *seed;
448  const void *personalizationData;
456  AESCTRDRBG_ReturnBehavior returnBehavior;
463  void *custom;
467 
474 
483 void AESCTRDRBG_init(void);
484 
492 
510 AESCTRDRBG_Handle AESCTRDRBG_open(uint_least8_t index, const AESCTRDRBG_Params *params);
511 
521 void AESCTRDRBG_close(AESCTRDRBG_Handle handle);
522 
546 int_fast16_t AESCTRDRBG_getBytes(AESCTRDRBG_Handle handle, CryptoKey *randomBytes);
547 
571 int_fast16_t AESCTRDRBG_generateKey(AESCTRDRBG_Handle handle, CryptoKey *randomKey);
572 
599 int_fast16_t AESCTRDRBG_getRandomBytes(AESCTRDRBG_Handle handle, void *randomBytes, size_t randomBytesSize);
600 
621 int_fast16_t AESCTRDRBG_reseed(AESCTRDRBG_Handle handle,
622  const void *seed,
623  const void *additionalData,
624  size_t additionalDataLength);
625 
649 AESCTRDRBG_Handle AESCTRDRBG_construct(AESCTRDRBG_Config *config, const AESCTRDRBG_Params *params);
650 
651 #ifdef __cplusplus
652 }
653 #endif
654 
655 #endif /* ti_drivers_AESCTRDRBG__include */
ADC_Params params
Definition: Driver_Init.h:11
The CryptoKey type is an opaque representation of a cryptographic key.
AESCTRDRBG_AES_KEY_LENGTH
Length in bytes of the internal AES key used by an instance.
Definition: AESCTRDRBG.h:359
#define AESCTRDRBG_AES_BLOCK_SIZE_BYTES
The AES block size in bytes.
Definition: AESCTRDRBG.h:354
void AESCTRDRBG_close(AESCTRDRBG_Handle handle)
Function to close an AESCTRDRBG instance specified by the AESCTRDRBG_Handle.
Definition: AESCTRDRBG.h:405
Definition: AESCTRDRBG.h:400
const AESCTRDRBG_Params AESCTRDRBG_defaultParams
Default AESCTRDRBG_Params structure.
AES Global configuration.
Definition: AESCommon.h:154
void AESCTRDRBG_init(void)
This function initializes the AESCTRDRBG driver.
CryptoKey datastructure.
Definition: CryptoKey.h:192
AESCTRDRBG_SEED_LENGTH
Length in bytes of seed used to instantiate or reseed instance.
Definition: AESCTRDRBG.h:368
Definition: AESCTRDRBG.h:371
Definition: AESCTR.h:496
int_fast16_t AESCTRDRBG_generateKey(AESCTRDRBG_Handle handle, CryptoKey *randomKey)
Populates the provided CryptoKey object&#39;s plaintext key-material with random bytes.
Definition: AESCTRDRBG.h:370
AESCTRDRBG_Handle AESCTRDRBG_construct(AESCTRDRBG_Config *config, const AESCTRDRBG_Params *params)
Constructs a new AESCTRDRBG object.
AESCTRDRBG Parameters.
Definition: AESCTRDRBG.h:434
AESCTRDRBG_ReturnBehavior
The way in which AESCTRDRBG function calls return after generating the requested entropy.
Definition: AESCTRDRBG.h:394
Definition: AESCTRDRBG.h:361
const void * seed
Definition: AESCTRDRBG.h:443
int_fast16_t AESCTRDRBG_getRandomBytes(AESCTRDRBG_Handle handle, void *randomBytes, size_t randomBytesSize)
Generates the requested number of random bytes and outputs to the given array.
void * custom
Definition: AESCTRDRBG.h:463
uint32_t reseedInterval
Definition: AESCTRDRBG.h:439
Definition: AESCTR.h:491
AESCommon_Config AESCTRDRBG_Config
AESCTRDRBG Global configuration.
Definition: AESCTRDRBG.h:419
AESCTRDRBG_Config * AESCTRDRBG_Handle
A handle that is returned from an AESCTRDRBG_open() call.
Definition: AESCTRDRBG.h:424
AESCTRDRBG_AES_KEY_LENGTH keyLength
Definition: AESCTRDRBG.h:436
int_fast16_t AESCTRDRBG_reseed(AESCTRDRBG_Handle handle, const void *seed, const void *additionalData, size_t additionalDataLength)
Reseed an AESCTRDRBG instance.
AESCTRDRBG_Handle AESCTRDRBG_open(uint_least8_t index, const AESCTRDRBG_Params *params)
This function opens a given AESCTRDRBG instance.
AES common module header for all devices.
int_fast16_t AESCTRDRBG_getBytes(AESCTRDRBG_Handle handle, CryptoKey *randomBytes)
Generates the requested number of random bytes.
AESCTRDRBG_ReturnBehavior returnBehavior
Definition: AESCTRDRBG.h:456
void AESCTRDRBG_Params_init(AESCTRDRBG_Params *params)
Function to initialize the AESCTRDRBG_Params struct to its defaults.
size_t personalizationDataLength
Definition: AESCTRDRBG.h:452
AESCTR driver header.
Definition: AESCTRDRBG.h:362
const void * personalizationData
Definition: AESCTRDRBG.h:448
© Copyright 1995-2022, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale