AESCTRDRBG.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 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 AESCTRDRBG.h
34  *
35  * @brief AESCTRDRBG driver header
36  *
37  * @warning This is a beta API. It may change in future releases.
38  *
39  * @anchor ti_drivers_AESCTRDRBG_Overview
40  * <h3> Overview </h3>
41  * AES_CTR_DRBG is a cryptographically secure deterministic random bit generator
42  * that is used to efficiently generate random numbers for use in keying material
43  * or other security related purposes. It is based on the AES block cipher
44  * operating in Counter (CTR) mode and is defined by NIST SP 800-90A.
45  *
46  * AES_CTR_DRBG derives a sequence of pseudo-random numbers based on an initial
47  * secret seed and additional, non-secret personalization data provided during
48  * instantiation. A sequence of random bits generated by AES_CTR_DRBG will have
49  * an equivalent entropy content of MIN(sequenceLength, security strength).
50  * The security strength is based on the seed length and the AES key length used
51  * in the AES_CTR_DRBG instance.
52  *
53  * | | AES-128 | AES-192 | AES-256 |
54  * |---------------------------------------|---------|---------|---------|
55  * | Security Strength (bits) | 128 | 192 | 256 |
56  * | Seed Length (bits) | 192 | 320 | 384 |
57  * | Personalization String Length (bits) | <= 192 | <= 320 | <= 384 |
58  * | Max Requests Between Reseeds | 2^48 | 2^48 | 2^48 |
59  * | Max Request Length (bits) | 2^19 | 2^19 | 2^19 |
60  *
61  * <h3> Security Strength </h3>
62  * The seed must be sourced from a cryptographically secure source such as
63  * a true random number generator and contain seed length bits of entropy.
64  * Since the seed length is always larger than the security strength for
65  * any one AES key length, the output of one AES_CTR_DRBG instance may not
66  * be used to seed another instance of the same or higher security strength.
67  *
68  * <h3> Reseeding </h3>
69  * Because of the way AES CTR operates, there are a limited number of output
70  * bitstreams that may be generated before the AES_CTR_DRBG instance must be
71  * reseeded. The time between reseeding is set by the number of random bit
72  * sequences generated not by their individual or combined lengths. Each time
73  * random bits are requested of the AES_CTR_DRBG instance by the application,
74  * the reseed counter is incremented by one regardless of how many bits at a
75  * time are requested. When this counter reaches the configured reseed limit,
76  * the AES_CTR_DRBG instance will return #AESCTRDRBG_STATUS_RESEED_REQUIRED
77  * until it is reseeded.
78  *
79  * The maximum permitted number of requests between reseeds is 2^48.
80  * The default counter is only 2^32 long for ease of implementation.
81  * A more conservative reseed limit may be configured by the application
82  * for increased security.
83  *
84  * A previously used seed may never be reused to reseed an AESCTRDRBG instance.
85  * The seed used to instantiate or reseed an instance must be generated by
86  * an approved entropy source and never be reused.
87  *
88  * <h3> Derivation Function </h3>
89  * NIST specifies the the use of an optional derivation function to reduced
90  * enctropy and personalizationg string lengths longer than the seed
91  * length down to the seed length. This feature is not presently supported.
92  *
93  * @anchor ti_drivers_AESCTRDRBG_Usage
94  * <h3> Usage </h3>
95  *
96  * This documentation provides a basic @ref ti_drivers_AESCTRDRBG_Synopsis
97  * "usage summary" and a set of @ref ti_drivers_AESCTRDRBG_Examples "examples"
98  * in the form of commented code fragments. Detailed descriptions of the
99  * APIs are provided in subsequent sections.
100  *
101  * @anchor ti_drivers_AESCTRDRBG_Synopsis
102  * <h3> Synopsis </h3>
103  * @anchor ti_drivers_AESCTRDRBG_Synopsis_Code
104  * @code
105  * #include <ti/drivers/AESCTRDRBG.h>
106  *
107  * AESCTRDRBG_init();
108  *
109  * // Instantiate the AESCTRDRBG instance
110  * AESCTRDRBG_Params_init(&params);
111  * params.keyLength = AESCTRDRBG_AES_KEY_LENGTH_128;
112  * params.reseedInterval = 0xFFFFFFFF;
113  * params.seed = seedBuffer;
114  *
115  * handle = AESCTRDRBG_open(0, &params);
116  *
117  * result = AESCTRDRBG_getBytes(handle, &resultKey);
118  *
119  * reseedResult = AESCTRDRBG_reseed(handle, reseedBuffer, NULL, 0);
120  *
121  * AESCTRDRBG_close(handle);
122  * @endcode
123  *
124  * @anchor ti_drivers_AESCTRDRBG_Examples
125  * <h3> Examples </h3>
126  *
127  * <h4> Instantiating an AESCTRDRBG Instance with TRNG </h4>
128  * @code
129  *
130  * #include <ti/drivers/AESCTRDRBG.h>
131  * #include <ti/drivers/TRNG.h>
132  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
133  *
134  * ...
135  *
136  * AESCTRDRBG_Handle handle;
137  * AESCTRDRBG_Params params;
138  * TRNG_Handle trngHandle;
139  * CryptoKey seedKey;
140  * int_fast16_t result;
141  *
142  * uint8_t seedBuffer[AESCTRDRBG_SEED_LENGTH_AES_128];
143  *
144  * // Generate the seed
145  * trngHandle = TRNG_open(0, NULL);
146  *
147  * if (trngHandle == NULL) {
148  * // Handle error
149  * while(1);
150  * }
151  *
152  * CryptoKeyPlaintext_initBlankKey(&seedKey, seedBuffer, AESCTRDRBG_SEED_LENGTH_AES_128);
153  *
154  * result = TRNG_generateEntropy(trngHandle, &seedKey);
155  * if (result != TRNG_STATUS_SUCCESS) {
156  * // Handle error
157  * while(1);
158  * }
159  *
160  * TRNG_close(trngHandle);
161  *
162  * // Instantiate the AESCTRDRBG instance
163  * AESCTRDRBG_Params_init(&params);
164  * params.keyLength = AESCTRDRBG_AES_KEY_LENGTH_128;
165  * params.reseedInterval = 0xFFFFFFFF;
166  * params.seed = seedBuffer;
167  *
168  * handle = AESCTRDRBG_open(0, &params);
169  * if (handle == NULL) {
170  * // Handle error
171  * while(1);
172  * }
173  * @endcode
174  *
175  * <h4> Generating Random Data with Reseeding </h4>
176  *
177  * @code
178  *
179  * #include <ti/drivers/AESCTRDRBG.h>
180  * #include <ti/drivers/TRNG.h>
181  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
182  *
183  * ...
184  *
185  * #define ENTROPY_REQUEST_LENGTH 256
186  *
187  * AESCTRDRBG_Handle handle;
188  * TRNG_Handle trngHandle;
189  * CryptoKey entropyKey;
190  * int_fast16_t result;
191  *
192  * uint8_t entropyBuffer[ENTROPY_REQUEST_LENGTH];
193  *
194  * // Initialise the AESCTRDRBG instance here
195  * ...
196  *
197  * // Start generating random numbers
198  * CryptoKeyPlaintext_initBlankKey(&entropyKey, entropyBuffer, ENTROPY_REQUEST_LENGTH);
199  *
200  * result = AESCTRDRBG_getBytes(handle, &resultKey);
201  *
202  * // Check return value and reseed if needed. This should happen only after many invocations
203  * // of AESCTRDRBG_getBytes().
204  * if (result == AESCTRDRBG_STATUS_RESEED_REQUIRED) {
205  * TRNG_Handle trngHandle;
206  * CryptoKey seedKey;
207  * int_fast16_t reseedResult;
208  * uint8_t reseedBuffer[AESCTRDRBG_SEED_LENGTH_AES_128];
209  *
210  * CryptoKeyPlaintext_initBlankKey(&seedKey, reseedBuffer, AESCTRDRBG_SEED_LENGTH_AES_128);
211  *
212  * reseedResult = TRNG_generateEntropy(trngHandle, &seedKey);
213  * if (reseedResult != TRNG_STATUS_SUCCESS) {
214  * // Handle error
215  * while(1);
216  * }
217  *
218  * TRNG_close(trngHandle);
219  *
220  * reseedResult = AESCTRDRBG_reseed(handle, reseedBuffer, NULL, 0);
221  * if (reseedResult != AESCTRDRBG_STATUS_SUCCESS) {
222  * // Handle error
223  * while(1);
224  * }
225  * }
226  * else if (result != AESCTRDRBG_STATUS_SUCCESS) {
227  * // Handle error
228  * while(1);
229  * }
230  *
231  * @endcode
232  *
233  */
234 
235 #ifndef ti_drivers_AESCTRDRBG__include
236 #define ti_drivers_AESCTRDRBG__include
237 
238 #include <stdbool.h>
239 #include <stddef.h>
240 #include <stdint.h>
241 
242 #include <ti/drivers/AESCTR.h>
244 
245 #ifdef __cplusplus
246 extern "C" {
247 #endif
248 
249 
262 #define AESCTRDRBG_STATUS_RESERVED (-32)
263 
270 #define AESCTRDRBG_STATUS_SUCCESS (0)
271 
278 #define AESCTRDRBG_STATUS_ERROR (-1)
279 
288 #define AESCTRDRBG_STATUS_RESOURCE_UNAVAILABLE (-2)
289 
296 #define AESCTRDRBG_STATUS_RESEED_REQUIRED (-3)
297 
301 #define AESCTRDRBG_AES_BLOCK_SIZE_BYTES 16
302 
306 typedef enum {
310 
314 typedef enum {
318 
339 typedef enum {
351 
363 typedef struct {
365  void *object;
366 
368  void const *hwAttrs;
370 
375 
384 typedef struct {
388  uint32_t reseedInterval;
392  const void *seed;
397  const void *personalizationData;
405  AESCTRDRBG_ReturnBehavior returnBehavior;
412  void *custom;
416 
423 
432 void AESCTRDRBG_init(void);
433 
441 
459 AESCTRDRBG_Handle AESCTRDRBG_open(uint_least8_t index, const AESCTRDRBG_Params *params);
460 
470 void AESCTRDRBG_close(AESCTRDRBG_Handle handle);
471 
485 int_fast16_t AESCTRDRBG_getBytes(AESCTRDRBG_Handle handle, CryptoKey *randomBytes);
486 
505 int_fast16_t AESCTRDRBG_reseed(AESCTRDRBG_Handle handle,
506  const void *seed,
507  const void *additionalData,
508  size_t additionalDataLength);
509 
533 AESCTRDRBG_Handle AESCTRDRBG_construct(AESCTRDRBG_Config *config, const AESCTRDRBG_Params *params);
534 
535 #ifdef __cplusplus
536 }
537 #endif
538 
539 #endif /* ti_drivers_AESCTRDRBG__include */
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:306
#define AESCTRDRBG_AES_BLOCK_SIZE_BYTES
The AES block size in bytes.
Definition: AESCTRDRBG.h:301
void AESCTRDRBG_close(AESCTRDRBG_Handle handle)
Function to close an AESCTRDRBG peripheral specified by the AESCTRDRBG_Handle.
Definition: AESCTRDRBG.h:349
Definition: AESCTRDRBG.h:344
const AESCTRDRBG_Params AESCTRDRBG_defaultParams
Default AESCTRDRBG_Params structure.
void AESCTRDRBG_init(void)
This function initializes the AESCTRDRBG driver.
void * object
Definition: AESCTRDRBG.h:365
CryptoKey datastructure.
Definition: CryptoKey.h:209
AESCTRDRBG Global configuration.
Definition: AESCTRDRBG.h:363
AESCTRDRBG_SEED_LENGTH
Length in bytes of seed used to instantiate or reseed instance.
Definition: AESCTRDRBG.h:314
Definition: AESCTRDRBG.h:316
Definition: AESCTR.h:391
Definition: AESCTRDRBG.h:315
AESCTRDRBG_Handle AESCTRDRBG_construct(AESCTRDRBG_Config *config, const AESCTRDRBG_Params *params)
Constructs a new AESCTRDRBG object.
AESCTRDRBG Parameters.
Definition: AESCTRDRBG.h:384
void const * hwAttrs
Definition: AESCTRDRBG.h:368
AESCTRDRBG_ReturnBehavior
The way in which AESCTRDRBG function calls return after generating the requested entropy.
Definition: AESCTRDRBG.h:339
Definition: AESCTRDRBG.h:307
const void * seed
Definition: AESCTRDRBG.h:392
void * custom
Definition: AESCTRDRBG.h:412
uint32_t reseedInterval
Definition: AESCTRDRBG.h:388
Definition: AESCTR.h:387
AESCTRDRBG_Config * AESCTRDRBG_Handle
A handle that is returned from an AESCTRDRBG_open() call.
Definition: AESCTRDRBG.h:374
AESCTRDRBG_AES_KEY_LENGTH keyLength
Definition: AESCTRDRBG.h:385
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.
int_fast16_t AESCTRDRBG_getBytes(AESCTRDRBG_Handle handle, CryptoKey *randomBytes)
Generate a requested number of random bytes.
AESCTRDRBG_ReturnBehavior returnBehavior
Definition: AESCTRDRBG.h:405
void AESCTRDRBG_Params_init(AESCTRDRBG_Params *params)
Function to initialize the AESCTRDRBG_Params struct to its defaults.
size_t personalizationDataLength
Definition: AESCTRDRBG.h:401
AESCTR driver header.
Definition: AESCTRDRBG.h:308
const void * personalizationData
Definition: AESCTRDRBG.h:397
© Copyright 1995-2019, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale