1 /*--------------------------------------------------------------------------*/
2 /* Copyright 2020-2021 NXP                                                  */
3 /*                                                                          */
4 /* NXP Confidential. This software is owned or controlled by NXP and may    */
5 /* only be used strictly in accordance with the applicable license terms.   */
6 /* By expressly accepting such terms or by downloading, installing,         */
7 /* activating and/or otherwise using the software, you are agreeing that    */
8 /* you have read, and that you agree to comply with and are bound by, such  */
9 /* license terms. If you do not agree to be bound by the applicable license */
10 /* terms, then you may not retain, install, activate or otherwise use the   */
11 /* software.                                                                */
12 /*--------------------------------------------------------------------------*/
13 
14 /**
15  * \file  mcuxCsslSecureCounter_SW_Local.h
16  * \brief SW implementation of the CSSL secure counter mechanism (using a local
17  *        variable).
18  */
19 
20 #ifndef MCUX_CSSL_SECURE_COUNTER_SW_LOCAL_H_
21 #define MCUX_CSSL_SECURE_COUNTER_SW_LOCAL_H_
22 
23 /**
24  * \addtogroup mcuxCsslIMPL MCUX CSSL -- Implementations
25  *
26  * \defgroup mcuxCsslSecureCounter_SwLocal Secure Counter: SW Local
27  * \brief Secure counter mechanism implementation using a local variable.
28  * \ingroup mcuxCsslIMPL
29  */
30 
31 
32 /**
33  * \defgroup scSwlCore Secure counter core functionality
34  * \brief Secure counter handling core functionality.
35  * \ingroup mcuxCsslSecureCounter_SwLocal
36  *
37  * \todo Extend this description of the core functionality.
38  */
39 
40 /**
41  * \def MCUX_CSSL_SC_COUNTER_NAME
42  * \brief Variable name to use for storing the secure counter value.
43  * \ingroup scSwlCore
44  */
45 #define MCUX_CSSL_SC_COUNTER_NAME \
46   mcuxCsslSecureCounter
47 
48 /****************************************************************************/
49 /* Constants                                                                */
50 /****************************************************************************/
51 
52 /**
53  * \def MCUX_CSSL_SC_CHECK_PASSED_IMPL
54  * \brief Positive comparison result value.
55  * \ingroup scSwlCore
56  */
57 #define MCUX_CSSL_SC_CHECK_PASSED_IMPL (0xA5A5A5A5u)
58 
59 /**
60  * \def MCUX_CSSL_SC_CHECK_FAILED_IMPL
61  * \brief Negative comparison result value.
62  * \ingroup scSwlCore
63  */
64 #define MCUX_CSSL_SC_CHECK_FAILED_IMPL (~ MCUX_CSSL_SC_CHECK_PASSED_IMPL)
65 
66 /**
67  * \def MCUX_CSSL_SC_COUNTER_TYPE_IMPL
68  * \brief Data type used for the secure counter.
69  * \ingroup scSwlCore
70  */
71 #define MCUX_CSSL_SC_COUNTER_TYPE_IMPL \
72   uint32_t
73 
74 /**
75  * \def MCUX_CSSL_SC_VALUE_TYPE_IMPL
76  * \brief Data type used for the secure counter values.
77  * \ingroup scSwlCore
78  */
79 #define MCUX_CSSL_SC_VALUE_TYPE_IMPL \
80   static const uint32_t
81 
82 /****************************************************************************/
83 /* Initialization                                                           */
84 /****************************************************************************/
85 
86 /**
87  * \def MCUX_CSSL_SC_ALLOC_IMPL
88  * \brief Allocation operation implementation for the secure counter.
89  * \ingroup scSwlCore
90  */
91 #define MCUX_CSSL_SC_ALLOC_IMPL() \
92   MCUX_CSSL_SC_COUNTER_TYPE_IMPL MCUX_CSSL_SC_COUNTER_NAME
93 
94 /**
95  * \def MCUX_CSSL_SC_INIT_IMPL
96  * \brief Initialization operation implementation for the secure counter.
97  * \ingroup scSwlCore
98  *
99  * \param value Value with which the secure counter must be initialized.
100  */
101 #define MCUX_CSSL_SC_INIT_IMPL(value) \
102   MCUX_CSSL_SC_ALLOC_IMPL() = (value)
103 
104 /****************************************************************************/
105 /* Check                                                                    */
106 /****************************************************************************/
107 
108 /**
109  * \def MCUX_CSSL_SC_CHECK_IMPL
110  * \brief Comparison operation implementation for the secure counter.
111  * \ingroup scSwlCore
112  *
113  * \param reference Reference value to compare the secure counter value against.
114  * \return          Either #MCUX_CSSL_SC_CHECK_PASSED, if the value matches, or
115  *                  #MCUX_CSSL_SC_CHECK_FAILED if the value is different.
116  */
117 #define MCUX_CSSL_SC_CHECK_IMPL(value) \
118   (MCUX_CSSL_SC_CHECK_FAILED_IMPL ^ (MCUX_CSSL_SC_COUNTER_NAME - ((value) + 1u)))
119 
120 /****************************************************************************/
121 /* Counter increment                                                        */
122 /****************************************************************************/
123 /**
124  * \defgroup scSwlInc Secure counter increment
125  * \brief Support for incrementing the secure counter.
126  * \ingroup mcuxCsslSecureCounter_SwLocal
127  */
128 
129 /**
130  * \def MCUX_CSSL_SC_ADD_IMPL
131  * \brief Increment the secure counter with \p value.
132  * \ingroup scSwlInc
133  *
134  * \see MCUX_CSSL_SC_SUB_IMPL
135  *
136  * \param value Value with which the secure counter must be incremented.
137  */
138 #define MCUX_CSSL_SC_ADD_IMPL(value) \
139   MCUX_CSSL_SC_COUNTER_NAME += (value)
140 
141 /**
142  * \def MCUX_CSSL_SC_ADD_0X1_IMPL
143  * \brief Increment the secure counter with 0x1.
144  * \ingroup scSwlInc
145  *
146  * \see MCUX_CSSL_SC_ADD_IMPL
147  */
148 #define MCUX_CSSL_SC_ADD_0X1_IMPL() \
149   MCUX_CSSL_SC_ADD_IMPL(0x1u)
150 
151 /**
152  * \def MCUX_CSSL_SC_ADD_0X10_IMPL
153  * \brief Increment the secure counter with 0x10.
154  * \ingroup scSwlInc
155  *
156  * \see MCUX_CSSL_SC_ADD_IMPL
157  */
158 #define MCUX_CSSL_SC_ADD_0X10_IMPL() \
159   MCUX_CSSL_SC_ADD_IMPL(0x10u)
160 
161 /**
162  * \def MCUX_CSSL_SC_ADD_0X100_IMPL
163  * \brief Increment the secure counter with 0x100.
164  * \ingroup scSwlInc
165  *
166  * \see MCUX_CSSL_SC_ADD_IMPL
167  */
168 #define MCUX_CSSL_SC_ADD_0X100_IMPL() \
169   MCUX_CSSL_SC_ADD(0x100u)
170 
171 /****************************************************************************/
172 /* Counter decrement                                                        */
173 /****************************************************************************/
174 /**
175  * \defgroup scSwlDec Secure counter decrement
176  * \brief Support for decrementing the secure counter.
177  * \ingroup mcuxCsslSecureCounter_SwLocal
178  */
179 
180 /**
181  * \def MCUX_CSSL_SC_SUB_IMPL
182  * \brief Decrement the secure counter with \p value.
183  * \ingroup scSwlDec
184  *
185  * \see MCUX_CSSL_SC_ADD_IMPL
186  *
187  * \param value Value with which the secure counter must be decremented.
188  */
189 #define MCUX_CSSL_SC_SUB_IMPL(value) \
190   MCUX_CSSL_SC_COUNTER_NAME -= (value)
191 
192 /**
193  * \def MCUX_CSSL_SC_SUB_0X1_IMPL
194  * \brief Decrement the secure counter with 0x1.
195  * \ingroup scSwlDec
196  *
197  * \see MCUX_CSSL_SC_SUB_IMPL
198  */
199 #define MCUX_CSSL_SC_SUB_0X1_IMPL() \
200   MCUX_CSSL_SC_SUB_IMPL(0x1u)
201 
202 /**
203  * \def MCUX_CSSL_SC_SUB_0X10_IMPL
204  * \brief Decrement the secure counter with 0x10.
205  * \ingroup scSwlDec
206  *
207  * \see MCUX_CSSL_SC_SUB_IMPL
208  */
209 #define MCUX_CSSL_SC_SUB_0X10_IMPL() \
210   MCUX_CSSL_SC_SUB_IMPL(0x10u)
211 
212 /**
213  * \def MCUX_CSSL_SC_SUB_0X100_IMPL
214  * \brief Decrement the secure counter with 0x100.
215  * \ingroup scSwlDec
216  *
217  * \see MCUX_CSSL_SC_SUB_IMPL
218  */
219 #define MCUX_CSSL_SC_SUB_0X100_IMPL() \
220   MCUX_CSSL_SC_SUB_IMPL(0x100u)
221 
222 /****************************************************************************/
223 /* Direct access (optional)                                                 */
224 /****************************************************************************/
225 /**
226  * \defgroup scSwlDirect Secure counter direct access
227  * \brief Support for directly accessing the secure counter.
228  * \ingroup mcuxCsslSecureCounter_SwLocal
229  *
230  * \warning Access to the secure counter is generally restricted, and generic
231  * access might not be allowed.
232  */
233 
234 /**
235  * \def MCUX_CSSL_SC_VALUE_IMPL
236  * \brief Access operation for the current secure counter value.
237  * \ingroup scSwlDirect
238  *
239  * \warning Access to the secure counter is generally restricted, and generic
240  * access might not be allowed. For portable code it is best to only rely on
241  * the check operation to verify the secure counter value.
242  *
243  * \return The current value of the secure counter.
244  */
245 #define MCUX_CSSL_SC_VALUE_IMPL() \
246   MCUX_CSSL_SC_COUNTER_NAME
247 
248 /**
249  * \def MCUX_CSSL_SC_ASSIGN_IMPL
250  * \brief Assignment operation for the secure counter.
251  * \ingroup scSwlDirect
252  *
253  * \warning Access to the secure counter is generally restricted, and generic
254  * assignment might not be allowed. For portable code it is best to only rely
255  * on the initialization, increment and decrement operations to change the
256  * secure counter value.
257  *
258  * \param value Value that needs to be assigned to the secure counter.
259  */
260 #define MCUX_CSSL_SC_ASSIGN_IMPL(value) \
261   MCUX_CSSL_SC_COUNTER_NAME = (value)
262 
263 
264 #endif /* MCUX_CSSL_SECURE_COUNTER_SW_LOCAL_H_ */
265