1 /*
2  * Copyright (c) 2019-2023, 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       AESCTRDRBGXX.h
34  *
35  *  @brief      Generic AESCTRDRBG implementation based on the AESCTR driver
36  *
37  * This file should only be included in the board file to fill the AESCTR_config
38  * struct.
39  *
40  *  # Use of AESCTR #
41  * This implementation uses the AESCTR driver to generate the random bitstream
42  * required to mutate the internal AESCTRDRBG state and provide random output
43  * bits. The driver will construct a dynamic instance of the AESCTR driver.
44  * Mutual exclusion and hardware access are all handled by the AESCTR driver
45  * instance.
46  *
47  *  # Implementation Limitations
48  * - Only plaintext CryptoKeys are supported by this implementation.
49  *
50  *  # Runtime Parameter Validation #
51  * The driver implementation does not perform runtime checks for most input
52  * parameters. Only values that are likely to have a stochastic element to them
53  * are checked (such as whether a driver is already open). Higher input
54  * parameter validation coverage is achieved by turning on assertions when
55  * compiling the driver.
56  */
57 
58 #ifndef ti_drivers_aesctrdrbg_AESCTRDRBGXX__include
59 #define ti_drivers_aesctrdrbg_AESCTRDRBGXX__include
60 
61 #include <stdint.h>
62 #include <stdbool.h>
63 
64 #include <ti/drivers/AESCTRDRBG.h>
65 #include <ti/devices/DeviceFamily.h>
66 #include DeviceFamily_constructPath(driverlib/aes.h)
67 
68 #if (DeviceFamily_PARENT == DeviceFamily_PARENT_CC23X0) || (DeviceFamily_PARENT == DeviceFamily_PARENT_CC27XX)
69     #include <ti/drivers/aesctr/AESCTRLPF3.h>
70 #else
71     #include <ti/drivers/aesctr/AESCTRCC26XX.h>
72 #endif
73 
74 #ifdef __cplusplus
75 extern "C" {
76 #endif
77 
78 /*! @brief Define that specifies the maximum AES key length required
79  *
80  *  This define defines what the largest AES key length used in an application is.
81  *  Since this implementation needs to support all AES key lengths by default,
82  *  temporary buffers and the internal driver state are sized to accommodate AES-256.
83  *  If only AES-128 is used in an application, the driver can be recompiled
84  *  with a different #AESCTRDRBG_MAX_KEY_LENGTH to save RAM in the #AESCTRDRBGXX_Object
85  *  and reducing stack size requirements.
86  */
87 #ifndef AESCTRDRBG_MAX_KEY_LENGTH
88     #define AESCTRDRBG_MAX_KEY_LENGTH AESCTRDRBG_AES_KEY_LENGTH_256
89 #endif
90 
91 /*! @brief Define that specifies the maximum seed length used by the driver */
92 #define AESCTRDRBG_MAX_SEED_LENGTH (AESCTRDRBG_MAX_KEY_LENGTH + AESCTRDRBG_AES_BLOCK_SIZE_BYTES)
93 
94 #if (ENABLE_KEY_STORAGE == 1) || (TFM_ENABLED == 1)
95     /*! @brief Maximum output key size in bytes when using KeyStore */
96     #define AESCTRDRBG_MAX_KEYSTORE_KEY_SIZE 64
97 #endif
98 
99 /*!
100  *  @brief      AESCTRDRBGXX Hardware Attributes
101  *
102  *  AESCTR26XX hardware attributes should be included in the board file
103  *  and pointed to by the AESCTR_config struct.
104  */
105 typedef struct
106 {
107     /*
108      * Priority in HWAttrs will be passed to AESCTR instance upon construct
109      */
110 #if (DeviceFamily_PARENT == DeviceFamily_PARENT_CC23X0) || (DeviceFamily_PARENT == DeviceFamily_PARENT_CC27XX)
111     AESCTRLPF3_HWAttrs aesctrHWAttrs;
112 #else
113     AESCTRCC26XX_HWAttrs aesctrHWAttrs;
114 #endif
115 } AESCTRDRBGXX_HWAttrs;
116 
117 /*!
118  *  @brief      AESCTRDRBGXX Object
119  *
120  *  The application must not access any member variables of this structure!
121  */
122 typedef struct
123 {
124     uint8_t keyingMaterial[AESCTRDRBG_AES_KEY_LENGTH_256];
125     uint8_t counter[AESCTRDRBG_AES_BLOCK_SIZE_BYTES];
126 #if (DeviceFamily_PARENT == DeviceFamily_PARENT_CC23X0) || (DeviceFamily_PARENT == DeviceFamily_PARENT_CC27XX)
127     AESCTRLPF3_Object aesctrObject;
128 #else
129     AESCTRCC26XX_Object aesctrObject;
130 #endif
131     AESCTR_Config ctrConfig;
132     AESCTR_Handle ctrHandle;
133     CryptoKey key;
134     size_t seedLength;
135     uint32_t reseedCounter;
136     uint32_t reseedInterval;
137     int_fast16_t returnStatus;
138     bool isOpen;
139     bool isInstantiated;
140 } AESCTRDRBGXX_Object;
141 
142 #ifdef __cplusplus
143 }
144 #endif
145 
146 #endif /* ti_drivers_aesctrdrbg_AESCTRDRBGXX__include */
147