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  mcuxCsslFlowProtection_SecureCounter_Common.h
16  * \brief Counter based implementation for the flow protection mechanism, for a local security counter.
17  */
18 
19 #ifndef MCUX_CSSL_FLOW_PROTECTION_SECURE_COUNTER_COMMON_H_
20 #define MCUX_CSSL_FLOW_PROTECTION_SECURE_COUNTER_COMMON_H_
21 
22 /* Include the CSSL C pre-processor support functionality. */
23 #include <internal/mcuxCsslCPreProcessor.h>
24 
25 /* Include the CSSL secure counter mechanism as basic building block. */
26 #include <mcuxCsslSecureCounter.h>
27 
28 /* Include the function identifier definitions. */
29 #include <mcuxCsslFlowProtection_FunctionIdentifiers.h>
30 
31 /* Include the C99 standard integer types. */
32 #include <stdint.h>
33 
34 /**
35  * \addtogroup mcuxCsslIMPL MCUX CSSL -- Implementations
36  *
37  * \defgroup mcuxCsslFlowProtection_SecureCounter Flow Protection: Secure Counter
38  * \brief Secure counter based implementation for the flow protection mechanism.
39  * \ingroup mcuxCsslIMPL
40  */
41 
42 
43 /**
44  * \defgroup csslFpCntCore Flow protection core functionality
45  * \brief Flow protection handling core functionality.
46  * \ingroup mcuxCsslFlowProtection_SecureCounter
47  *
48  * \todo Extend this description of the core functionality which relies
49  *       basically on the function calling flow protection.
50  *
51  * \declaration{MCUX_CSSL_FP_FUNCTION_DECL_IMPL}
52  * \event{MCUX_CSSL_FP_FUNCTION_CALL_IMPL}
53  * \expectation{MCUX_CSSL_FP_FUNCTION_CALLED_IMPL}
54  */
55 
56 /**
57  * \def MCUX_CSSL_FP_DECL_NAME
58  * \brief Construct a name based on type and id.
59  * \ingroup csslFpCntCore
60  *
61  * \param type Indicator for the type of declaration.
62  * \param id   Identifier for the flow protected entity.
63  * \return     CSSL flow protection entity name for given \p type and \p id.
64  */
65 #define MCUX_CSSL_FP_DECL_NAME(type, id) \
66   MCUX_CSSL_CPP_CAT4(mcuxCsslFlowProtection_, type, _, id)
67 
68 /**
69  * \def MCUX_CSSL_FP_DECL_IMPL
70  * \brief Generic flow protected entity declaration implementation.
71  * \ingroup csslFpCntCore
72  *
73  * \param type Indicator for the type of declaration.
74  * \param id   Identifier for the flow protected entity.
75  * \return     CSSL flow protection entity declaration.
76  */
77 #define MCUX_CSSL_FP_DECL_IMPL(type, id) \
78   MCUX_CSSL_SC_VALUE_TYPE MCUX_CSSL_FP_DECL_NAME(type, id) = \
79     MCUX_CSSL_CPP_CAT3(MCUX_CSSL_FP_, type, _ID)(id)
80 
81 /**
82  * \def MCUX_CSSL_FP_ID_IMPL
83  * \brief Generic identifier generator based on current line number.
84  * \ingroup csslFpCntCore
85  *
86  * \return Counter value based on the current line number.
87  */
88 #define MCUX_CSSL_FP_ID_IMPL() \
89   MCUX_CSSL_CPP_CAT(__LINE__, u)
90 
91 
92 
93 /**
94  * \defgroup csslFpCntExpect Expectation handling
95  * \brief Expectation handling support functionality.
96  * \ingroup mcuxCsslFlowProtection_SecureCounter
97  */
98 
99 /**
100  * \def MCUX_CSSL_FP_EXPECTATIONS
101  * \brief Expectation aggregation.
102  * \ingroup csslFpCntExpect
103  *
104  * \param expect One or more (comma separated) declarations of expected code
105  *               flow behavior.
106  * \return       Aggregated counter value for the given expectations.
107  */
108 #define MCUX_CSSL_FP_EXPECTATIONS(...) \
109   (MCUX_CSSL_CPP_MAP(MCUX_CSSL_CPP_ADD, __VA_ARGS__))
110 
111 /**
112  * @def MCUX_CSSL_FP_COUNTER_STMT
113  * @brief A statement which is only evaluated if a secure counter is used.
114  * @api
115  * @ingroup csslFpCntCore
116  *
117  * This macro can be used to create counting variables that are only present if
118  * the active configuration uses a secure counter, to avoid warnings about
119  * unused variables.
120  *
121  * @param statement The statement to be conditionally included.
122  */
123 #define MCUX_CSSL_FP_COUNTER_STMT_IMPL(statement) \
124   statement
125 
126 /**
127  * \def MCUX_CSSL_FP_CONDITIONAL_IMPL
128  * \brief Conditional expectation aggregation.
129  * \ingroup csslFpCntCore
130  *
131  * \param condition Condition under which the given expectations apply
132  * \param expect    One or more (comma separated) declarations of expected code
133  *                  code flow behavior.
134  * \return          Aggregated counter value for the given expectations, if
135  *                  condition is satisfied. Otherwise 0.
136  */
137 #define MCUX_CSSL_FP_CONDITIONAL_IMPL(condition, ...) \
138   (MCUX_CSSL_FP_EXPECTATIONS(__VA_ARGS__) & ((condition) ? ((uint32_t) UINT32_MAX) : ((uint32_t) 0)))
139 
140 /**
141  * \def MCUX_CSSL_FP_EXPECT_IMPL
142  * \brief Declaration(s) of expected code flow behavior.
143  * \ingroup csslFpCntExpect
144  *
145  * This macro can be used to indicate expectations in the function body at
146  * another location than the function entry or exit.
147  *
148  * \see MCUX_CSSL_FP_EXPECTATIONS
149  *
150  * \param expect One or more (comma separated) declarations of expected code
151  *               flow behavior.
152  */
153 #define MCUX_CSSL_FP_EXPECT_IMPL(...) \
154   MCUX_CSSL_SC_SUB( \
155     MCUX_CSSL_FP_EXPECTATIONS(__VA_ARGS__) \
156   )
157 
158 
159 
160 /**
161  * \defgroup csslFpCntFunction Function calling flow protection
162  * \brief Support for flow protected functions.
163  * \ingroup mcuxCsslFlowProtection_SecureCounter
164  *
165  * \declaration{MCUX_CSSL_FP_FUNCTION_DECL_IMPL}
166  * \event{MCUX_CSSL_FP_FUNCTION_CALL_IMPL}
167  * \expectation{MCUX_CSSL_FP_FUNCTION_CALLED_IMPL}
168  */
169 
170 /**
171  * \def MCUX_CSSL_FP_FUNCTION_ID
172  * \brief Generator for function identifiers.
173  * \ingroup csslFpCntFunction
174  *
175  * \param id Identifier for the flow protected function.
176  */
177 #define MCUX_CSSL_FP_FUNCTION_ID(id) \
178   MCUX_CSSL_CPP_CAT(MCUX_CSSL_FP_FUNCID_, id)
179 
180 /**
181  * \def MCUX_CSSL_FP_FUNCTION_ID_ENTRY_MASK
182  * \brief Mask to be used to derive entry part from a function identifier
183  * \ingroup csslFpCntFunction
184  */
185 #define MCUX_CSSL_FP_FUNCTION_ID_ENTRY_MASK \
186   (0x5A5A5A5Au)
187 
188 /**
189  * \def MCUX_CSSL_FP_FUNCTION_ID_ENTRY_PART
190  * \brief Part of the function identifier to be used at function entry.
191  * \ingroup csslFpCntFunction
192  *
193  * \param id Identifier for the flow protected function.
194  */
195 #define MCUX_CSSL_FP_FUNCTION_ID_ENTRY_PART(id) \
196   (MCUX_CSSL_FP_FUNCTION_VALUE(id) & MCUX_CSSL_FP_FUNCTION_ID_ENTRY_MASK)
197 
198 /**
199  * \def MCUX_CSSL_FP_FUNCTION_ID_EXIT_PART
200  * \brief Part of the function identifier to be used at function exit.
201  * \ingroup csslFpCntFunction
202  *
203  * \param id Identifier for the flow protected function.
204  */
205 #define MCUX_CSSL_FP_FUNCTION_ID_EXIT_PART(id) \
206   (MCUX_CSSL_FP_FUNCTION_VALUE(id) - MCUX_CSSL_FP_FUNCTION_ID_ENTRY_PART(id))
207 
208 /**
209  * \def MCUX_CSSL_FP_FUNCTION_DECL_IMPL
210  * \brief Declaration implementation of a flow protected function.
211  * \ingroup csslFpCntFunction
212  *
213  * \event{MCUX_CSSL_FP_FUNCTION_CALL_IMPL}
214  * \expectation{MCUX_CSSL_FP_FUNCTION_CALLED_IMPL}
215  *
216  * \param id Identifier for the function that is flow protected.
217  */
218 #define MCUX_CSSL_FP_FUNCTION_DECL_IMPL(id) \
219   /* Intentionally empty */
220 
221 /**
222  * \def MCUX_CSSL_FP_FUNCTION_VALUE
223  * \brief Macro to get the value for a given function.
224  * \ingroup csslFpCntFunction
225  *
226  * \param id Identifier for the function that is flow protected.
227  * \return   The counter value for the given function \p id.
228  */
229 #define MCUX_CSSL_FP_FUNCTION_VALUE(id) \
230   MCUX_CSSL_FP_FUNCTION_ID(id)
231 
232 /**
233  * \def MCUX_CSSL_FP_FUNCTION_DEF_IMPL
234  * \brief Definition implementation of a flow protected function.
235  * \ingroup csslFpCntFunction
236  *
237  * Not used in the current implementation.
238  *
239  * \param id Identifier for the function that is flow protected.
240  */
241 #define MCUX_CSSL_FP_FUNCTION_DEF_IMPL(id) \
242   /* Intentionally empty. */
243 
244 /**
245  * \def MCUX_CSSL_FP_RESULT_OFFSET
246  * \brief Offset of the result in the return value.
247  * \ingroup csslFpCntFunction
248  */
249 #define MCUX_CSSL_FP_RESULT_OFFSET \
250   (0u)
251 
252 /**
253  * \def MCUX_CSSL_FP_RESULT_MASK
254  * \brief Bitmask of the result in the return value.
255  * \ingroup csslFpCntFunction
256  */
257 #define MCUX_CSSL_FP_RESULT_MASK \
258   (0xFFFFFFFFuLL)
259 
260 /**
261  * \def MCUX_CSSL_FP_RESULT_VALUE
262  * \brief Encode a result value for a protected return value.
263  * \ingroup csslFpCntFunction
264  *
265  * \param result The result that needs to be encoded.
266  */
267 #define MCUX_CSSL_FP_RESULT_VALUE(result) \
268   (((uint64_t)(result) & MCUX_CSSL_FP_RESULT_MASK) << MCUX_CSSL_FP_RESULT_OFFSET)
269 
270 /**
271  * \def MCUX_CSSL_FP_RESULT_IMPL
272  * \brief Extract the result value from a protected \p return value.
273  * \ingroup csslFpCntFunction
274  *
275  * \param return The protected return value which contains the result.
276  */
277 #define MCUX_CSSL_FP_RESULT_IMPL(return) \
278   (uint32_t)(((return) >> MCUX_CSSL_FP_RESULT_OFFSET) & MCUX_CSSL_FP_RESULT_MASK)
279 
280 /**
281  * \def MCUX_CSSL_FP_PROTECTION_OFFSET
282  * \brief Offset of the protection token in the return value.
283  * \ingroup csslFpCntFunction
284  */
285 #define MCUX_CSSL_FP_PROTECTION_OFFSET \
286   (32u)
287 
288 /**
289  * \def MCUX_CSSL_FP_PROTECTION_MASK
290  * \brief Bitmask of the protection token in the return value.
291  * \ingroup csslFpCntFunction
292  */
293 #define MCUX_CSSL_FP_PROTECTION_MASK \
294   (0xFFFFFFFFuLL)
295 
296 /**
297  * \def MCUX_CSSL_FP_PROTECTION_TOKEN_VALUE
298  * \brief Encode a protection token for a protected return value.
299  * \ingroup csslFpCntFunction
300  *
301  * Note that this macro is only used with a local security counter,
302  * e.g. for configuration CSSL_SC_USE_SW_LOCAL
303  *
304  * \param token The protection token that needs to be encoded.
305  */
306 #define MCUX_CSSL_FP_PROTECTION_TOKEN_VALUE(token) \
307   ((((uint64_t)(token) & MCUX_CSSL_FP_PROTECTION_MASK)) << MCUX_CSSL_FP_PROTECTION_OFFSET)
308 
309 /**
310  * \def MCUX_CSSL_FP_PROTECTION_TOKEN_IMPL
311  * \brief Extract the protection token value from a protected \p return value.
312  * \ingroup csslFpCntFunction
313  *
314  * Note that this macro is only used with a local security counter,
315  * e.g. for configuration CSSL_SC_USE_SW_LOCAL
316  *
317  * \param return The protected return value which contains the protection token.
318  */
319 #define MCUX_CSSL_FP_PROTECTION_TOKEN_IMPL(return) \
320   (uint32_t)(((return) >> MCUX_CSSL_FP_PROTECTION_OFFSET) & MCUX_CSSL_FP_PROTECTION_MASK)
321 
322 /**
323  * \def MCUX_CSSL_FP_COUNTER_COMPRESSED
324  * \brief Compressed version of the secure counter that can be used as a
325  *        protection token.
326  * \ingroup csslFpCntFunction
327  */
328 #define MCUX_CSSL_FP_COUNTER_COMPRESSED() \
329   MCUX_CSSL_SC_VALUE()
330 
331 
332 /**
333  * \def MCUX_CSSL_FP_FUNCTION_CALLED_IMPL
334  * \brief Expectation implementation of a called function.
335  * \ingroup csslFpCntFunction
336  *
337  * \declaration{MCUX_CSSL_FP_FUNCTION_DECL_IMPL}
338  * \event{MCUX_CSSL_FP_FUNCTION_CALL_IMPL}
339  *
340  * \see MCUX_CSSL_FP_FUNCTION_VALUE
341  *
342  * \param id Identifier of the function that is expected to be called.
343  * \return   Counter value for the given function.
344  */
345 #define MCUX_CSSL_FP_FUNCTION_CALLED_IMPL(id) \
346   MCUX_CSSL_FP_FUNCTION_VALUE(id)
347 
348 
349 
350 /**
351  * \defgroup csslFpCntLoop Looping flow protection
352  * \brief Support for flow protected loops.
353  * \ingroup mcuxCsslFlowProtection_SecureCounter
354  *
355  * \declaration{MCUX_CSSL_FP_LOOP_DECL_IMPL}
356  * \event{MCUX_CSSL_FP_LOOP_ITERATION_IMPL}
357  * \expectation{MCUX_CSSL_FP_LOOP_ITERATIONS_IMPL}
358  */
359 
360 /**
361  * \def MCUX_CSSL_FP_LOOP_ID
362  * \brief Generator for loop identifiers.
363  * \ingroup csslFpCntLoop
364  *
365  * \param id Identifier for the flow protected loop.
366  * \return   Counter value for the given loop.
367  */
368 #define MCUX_CSSL_FP_LOOP_ID(id) \
369   MCUX_CSSL_FP_ID_IMPL()
370 
371 /**
372  * \def MCUX_CSSL_FP_LOOP_DECL_IMPL
373  * \brief Declaration implementation of a flow protected loop.
374  * \ingroup csslFpCntLoop
375  *
376  * \param id Identifier for the loop that is flow protected.
377  */
378 #define MCUX_CSSL_FP_LOOP_DECL_IMPL(id) \
379   MCUX_CSSL_FP_DECL_IMPL(LOOP, id)
380 
381 /**
382  * \def MCUX_CSSL_FP_LOOP_VALUE
383  * \brief Macro to get the value for a given loop.
384  * \ingroup csslFpCntLoop
385  *
386  * \param id Identifier for the loop that is flow protected.
387  * \return   The counter value for the given loop \p id.
388  */
389 #define MCUX_CSSL_FP_LOOP_VALUE(id) \
390   MCUX_CSSL_FP_DECL_NAME(LOOP, id)
391 
392 /**
393  * \def MCUX_CSSL_FP_LOOP_ITERATION_IMPLn
394  * \brief Event implementation of a loop iteration (with expectations).
395  * \ingroup csslFpCntLoop
396  *
397  * \see MCUX_CSSL_FP_LOOP_ITERATION_IMPL
398  *
399  * \param id     Identifier for the loop that is flow protected.
400  * \param expect One or more (comma separated) declarations of expected code
401  *               flow behavior related to this event.
402  */
403 #define MCUX_CSSL_FP_LOOP_ITERATION_IMPLn(id, ...) \
404   MCUX_CSSL_SC_ADD( \
405     MCUX_CSSL_FP_LOOP_VALUE(id) \
406     - MCUX_CSSL_FP_EXPECTATIONS(__VA_ARGS__) \
407   )
408 
409 /**
410  * \def MCUX_CSSL_FP_LOOP_ITERATION_IMPL1
411  * \brief Event implementation of a loop iteration (without expectations).
412  * \ingroup csslFpCntLoop
413  *
414  * \see MCUX_CSSL_FP_LOOP_ITERATION_IMPL
415  * \see MCUX_CSSL_FP_LOOP_ITERATION_IMPLn
416  *
417  * \param id Identifier for the loop that is flow protected.
418  */
419 #define MCUX_CSSL_FP_LOOP_ITERATION_IMPL1(id) \
420   MCUX_CSSL_FP_LOOP_ITERATION_IMPLn(id, 0u)
421 
422 /**
423  * \def MCUX_CSSL_FP_LOOP_ITERATION_IMPL
424  * \brief Event implementation of a loop iteration.
425  * \ingroup csslFpCntLoop
426  *
427  * Implemented as an overloaded macro to simplify the use of the API.
428  *
429  * \see MCUX_CSSL_FP_LOOP_ITERATION_IMPL1
430  * \see MCUX_CSSL_FP_LOOP_ITERATION_IMPLn
431  *
432  * \param id     Identifier for the loop that is flow protected.
433  * \param expect Zero or more (comma separated) declarations of expected code
434  *               flow behavior related to this event.
435  */
436 #define MCUX_CSSL_FP_LOOP_ITERATION_IMPL(...) \
437   MCUX_CSSL_CPP_OVERLOADED1(MCUX_CSSL_FP_LOOP_ITERATION_IMPL, __VA_ARGS__)
438 
439 /**
440  * \def MCUX_CSSL_FP_LOOP_ITERATIONS_IMPL
441  * \brief Expectation implementation of a number of loop iterations.
442  * \ingroup csslFpCntLoop
443  *
444  * \param id    Identifier of the flow protected loop.
445  * \param count Number of expected iterations.
446  */
447 #define MCUX_CSSL_FP_LOOP_ITERATIONS_IMPL(id, count) \
448   ((count) * MCUX_CSSL_FP_LOOP_VALUE(id))
449 
450 
451 
452 /**
453  * \defgroup csslFpCntBranch Branching flow protection
454  * \brief Support for flow protected branches.
455  * \ingroup mcuxCsslFlowProtection_SecureCounter
456  *
457  * \declaration{MCUX_CSSL_FP_BRANCH_DECL_IMPL}
458  * \event{MCUX_CSSL_FP_BRANCH_POSITIVE_IMPL,MCUX_CSSL_FP_BRANCH_NEGATIVE_IMPL}
459  * \expectation{MCUX_CSSL_FP_BRANCH_TAKEN_POSITIVE_IMPL,MCUX_CSSL_FP_BRANCH_TAKEN_NEGATIVE_IMPL}
460  */
461 
462 /**
463  * \def MCUX_CSSL_FP_BRANCH_ID
464  * \brief Generator for branch identifiers.
465  * \ingroup csslFpCntBranch
466  *
467  * \param id Identifier for the flow protected branch.
468  * \return   Counter value for the given branch.
469  */
470 #define MCUX_CSSL_FP_BRANCH_ID(id) \
471   MCUX_CSSL_FP_ID_IMPL()
472 
473 /**
474  * \def MCUX_CSSL_FP_BRANCH_DECL_IMPL
475  * \brief Declaration implementation of a flow protected branch.
476  * \ingroup csslFpCntBranch
477  *
478  * \param id Identifier for the branch that is flow protected.
479  */
480 #define MCUX_CSSL_FP_BRANCH_DECL_IMPL(id) \
481   MCUX_CSSL_FP_DECL_IMPL(BRANCH, id)
482 
483 /**
484  * \def MCUX_CSSL_FP_BRANCH_VALUE
485  * \brief Macro to get the value for a given branch.
486  * \ingroup csslFpCntBranch
487  *
488  * \param id Identifier for the branch that is flow protected.
489  * \return   The counter value for the given branch \p id.
490  */
491 #define MCUX_CSSL_FP_BRANCH_VALUE(id) \
492   MCUX_CSSL_FP_DECL_NAME(BRANCH, id)
493 
494 /**
495  * \def MCUX_CSSL_FP_BRANCH_POSITIVE_VALUE
496  * \brief Value to use for the positive scenario.
497  * \ingroup csslFpCntBranch
498  */
499 #define MCUX_CSSL_FP_BRANCH_POSITIVE_VALUE 0x5u
500 
501 /**
502  * \def MCUX_CSSL_FP_BRANCH_NEGATIVE_VALUE
503  * \brief Value to use for the negative scenario.
504  * \ingroup csslFpCntBranch
505  */
506 #define MCUX_CSSL_FP_BRANCH_NEGATIVE_VALUE 0xAu
507 
508 /**
509  * \def MCUX_CSSL_FP_BRANCH_SCENARIO_IMPL
510  * \brief Event implementation for the execution of a specified branch scenario.
511  * \ingroup csslFpCntBranch
512  *
513  * \see MCUX_CSSL_FP_BRANCH_POSITIVE_IMPL
514  * \see MCUX_CSSL_FP_BRANCH_NEGATIVE_IMPL
515  *
516  * \param id       Identifier for the branch for which the given \p scenario is
517  *                 executed.
518  * \param scenario The scenario for a branch is either positive or negative.
519  * \param expect   One or more (comma separated) declarations of expected code
520  *                 flow behavior related to this event.
521  */
522 #define MCUX_CSSL_FP_BRANCH_SCENARIO_IMPL(id, scenario, ...) \
523   MCUX_CSSL_SC_ADD( \
524     (MCUX_CSSL_FP_BRANCH_VALUE(id) * (scenario)) \
525     - MCUX_CSSL_FP_EXPECTATIONS(__VA_ARGS__) \
526   )
527 
528 /**
529  * \def MCUX_CSSL_FP_BRANCH_POSITIVE_IMPLn
530  * \brief Event implementation for the execution of a positive branch scenario
531  *        (with expectations).
532  * \ingroup csslFpCntBranch
533  *
534  * \see MCUX_CSSL_FP_BRANCH_POSITIVE_IMPL
535  * \see MCUX_CSSL_FP_BRANCH_POSITIVE_IMPL1
536  *
537  * \param id     Identifier for the branch for which the positive scenario is
538  *               executed.
539  * \param expect One or more (comma separated) declarations of expected code
540  *               flow behavior related to this event.
541  */
542 #define MCUX_CSSL_FP_BRANCH_POSITIVE_IMPLn(id, ...) \
543   MCUX_CSSL_FP_BRANCH_SCENARIO_IMPL(id, MCUX_CSSL_FP_BRANCH_POSITIVE_VALUE, \
544     __VA_ARGS__)
545 
546 /**
547  * \def MCUX_CSSL_FP_BRANCH_POSITIVE_IMPL1
548  * \brief Event implementation for the execution of a positive branch scenario
549  *        (without expectations).
550  * \ingroup csslFpCntBranch
551  *
552  * \see MCUX_CSSL_FP_BRANCH_POSITIVE_IMPL
553  * \see MCUX_CSSL_FP_BRANCH_POSITIVE_IMPLn
554  *
555  * \param id Identifier for the branch for which the positive scenario is
556  *           executed.
557  */
558 #define MCUX_CSSL_FP_BRANCH_POSITIVE_IMPL1(id) \
559   MCUX_CSSL_FP_BRANCH_SCENARIO_IMPL(id, MCUX_CSSL_FP_BRANCH_POSITIVE_VALUE, 0u)
560 
561 /**
562  * \def MCUX_CSSL_FP_BRANCH_POSITIVE_IMPL
563  * \brief Event implementation for the execution of a positive branch scenario.
564  * \ingroup csslFpCntBranch
565  *
566  * Implemented as an overloaded macro to simplify the use of the API.
567  *
568  * \see MCUX_CSSL_FP_BRANCH_POSITIVE_IMPL1
569  * \see MCUX_CSSL_FP_BRANCH_POSITIVE_IMPLn
570  *
571  * \param id     Identifier for the branch for which the positive scenario is
572  *               executed.
573  * \param expect Zero or more (comma separated) declarations of expected code
574  *               flow behavior related to this event.
575  */
576 #define MCUX_CSSL_FP_BRANCH_POSITIVE_IMPL(...) \
577   MCUX_CSSL_CPP_OVERLOADED1(MCUX_CSSL_FP_BRANCH_POSITIVE_IMPL, __VA_ARGS__)
578 
579 /**
580  * \def MCUX_CSSL_FP_BRANCH_NEGATIVE_IMPLn
581  * \brief Event implementation for the execution of a negative branch scenario
582  *        (with expectations).
583  * \ingroup csslFpCntBranch
584  *
585  * \see MCUX_CSSL_FP_BRANCH_NEGATIVE_IMPL
586  * \see MCUX_CSSL_FP_BRANCH_NEGATIVE_IMPL1
587  *
588  * \param id     Identifier for the branch for which the negative scenario is
589  *               executed.
590  * \param expect One or more (comma separated) declarations of expected code
591  *               flow behavior related to this event.
592  */
593 #define MCUX_CSSL_FP_BRANCH_NEGATIVE_IMPLn(id, ...) \
594   MCUX_CSSL_FP_BRANCH_SCENARIO_IMPL(id, MCUX_CSSL_FP_BRANCH_NEGATIVE_VALUE, \
595     __VA_ARGS__)
596 
597 /**
598  * \def MCUX_CSSL_FP_BRANCH_NEGATIVE_IMPL1
599  * \brief Event implementation for the execution of a negative branch scenario
600  *        (without expectations).
601  * \ingroup csslFpCntBranch
602  *
603  * \see MCUX_CSSL_FP_BRANCH_NEGATIVE_IMPL
604  * \see MCUX_CSSL_FP_BRANCH_NEGATIVE_IMPLn
605  *
606  * \param id Identifier for the branch for which the negative scenario is
607  *           executed.
608  */
609 #define MCUX_CSSL_FP_BRANCH_NEGATIVE_IMPL1(id) \
610   MCUX_CSSL_FP_BRANCH_SCENARIO_IMPL(id, MCUX_CSSL_FP_BRANCH_NEGATIVE_VALUE, 0u)
611 
612 /**
613  * \def MCUX_CSSL_FP_BRANCH_NEGATIVE_IMPL
614  * \brief Event implementation for the execution of a negative branch scenario.
615  * \ingroup csslFpCntBranch
616  *
617  * Implemented as an overloaded macro to simplify the use of the API.
618  *
619  * \see MCUX_CSSL_FP_BRANCH_NEGATIVE_IMPL1
620  * \see MCUX_CSSL_FP_BRANCH_NEGATIVE_IMPLn
621  *
622  * \param id     Identifier for the branch for which the negative scenario is
623  *               executed.
624  * \param expect Zero or more (comma separated) declarations of expected code
625  *               flow behavior related to this event.
626  */
627 #define MCUX_CSSL_FP_BRANCH_NEGATIVE_IMPL(...) \
628   MCUX_CSSL_CPP_OVERLOADED1(MCUX_CSSL_FP_BRANCH_NEGATIVE_IMPL, __VA_ARGS__)
629 
630 /**
631  * \def MCUX_CSSL_FP_BRANCH_TAKEN_IMPL
632  * \brief Expectation implementation of an executed specified branch scenario.
633  * \ingroup csslFpCntBranch
634  *
635  * \see MCUX_CSSL_FP_BRANCH_TAKEN_POSITIVE_IMPL
636  * \see MCUX_CSSL_FP_BRANCH_TAKEN_NEGATIVE_IMPL
637  *
638  * \param id        Identifier of the flow protected branch.
639  * \param scenario The scenario for a branch is either positive or negative.
640  * \param condition Condition under which this branch is taken.
641  */
642 #define MCUX_CSSL_FP_BRANCH_TAKEN_IMPL(id, scenario, condition) \
643   MCUX_CSSL_FP_CONDITIONAL_IMPL(condition, \
644     MCUX_CSSL_FP_BRANCH_VALUE(id) * (scenario))
645 
646 /**
647  * \def MCUX_CSSL_FP_BRANCH_TAKEN_POSITIVE_IMPL2
648  * \brief Expectation implementation of an executed positive branch (with
649  *        condition).
650  * \ingroup csslFpCntBranch
651  *
652  * \see MCUX_CSSL_FP_BRANCH_TAKEN_POSITIVE_IMPL
653  * \see MCUX_CSSL_FP_BRANCH_TAKEN_POSITIVE_IMPL1
654  *
655  * \param id        Identifier of the flow protected branch.
656  * \param condition Condition under which this branch is taken.
657  */
658 #define MCUX_CSSL_FP_BRANCH_TAKEN_POSITIVE_IMPL2(id, condition) \
659   MCUX_CSSL_FP_BRANCH_TAKEN_IMPL(id, MCUX_CSSL_FP_BRANCH_POSITIVE_VALUE, condition)
660 
661 /**
662  * \def MCUX_CSSL_FP_BRANCH_TAKEN_POSITIVE_IMPL1
663  * \brief Expectation implementation of an executed positive branch (without
664  *        condition).
665  * \ingroup csslFpCntBranch
666  *
667  * \see MCUX_CSSL_FP_BRANCH_TAKEN_POSITIVE_IMPL
668  * \see MCUX_CSSL_FP_BRANCH_TAKEN_POSITIVE_IMPL2
669  *
670  * \param id Identifier of the flow protected branch.
671  */
672 #define MCUX_CSSL_FP_BRANCH_TAKEN_POSITIVE_IMPL1(id) \
673   MCUX_CSSL_FP_BRANCH_TAKEN_IMPL(id, MCUX_CSSL_FP_BRANCH_POSITIVE_VALUE, true)
674 
675 /**
676  * \def MCUX_CSSL_FP_BRANCH_TAKEN_POSITIVE_IMPL
677  * \brief Expectation implementation of an executed positive branch.
678  * \ingroup csslFpCntBranch
679  *
680  * Implemented as an overloaded macro to simplify the use of the API.
681  *
682  * \see MCUX_CSSL_FP_BRANCH_TAKEN_POSITIVE_IMPL1
683  * \see MCUX_CSSL_FP_BRANCH_TAKEN_POSITIVE_IMPL2
684  *
685  * \param id        Identifier of the flow protected branch.
686  * \param condition Optional, condition under which this branch is taken.
687  */
688 #define MCUX_CSSL_FP_BRANCH_TAKEN_POSITIVE_IMPL(...) \
689   MCUX_CSSL_CPP_OVERLOADED2(MCUX_CSSL_FP_BRANCH_TAKEN_POSITIVE_IMPL, __VA_ARGS__)
690 
691 /**
692  * \def MCUX_CSSL_FP_BRANCH_TAKEN_NEGATIVE_IMPL2
693  * \brief Expectation implementation of an executed negative branch (with
694  *        condition).
695  * \ingroup csslFpCntBranch
696  *
697  * \see MCUX_CSSL_FP_BRANCH_TAKEN_NEGATIVE_IMPL
698  * \see MCUX_CSSL_FP_BRANCH_TAKEN_NEGATIVE_IMPL1
699  *
700  * \param id        Identifier of the flow protected branch.
701  * \param condition Condition under which this branch is taken.
702  */
703 #define MCUX_CSSL_FP_BRANCH_TAKEN_NEGATIVE_IMPL2(id, condition) \
704   MCUX_CSSL_FP_BRANCH_TAKEN_IMPL(id, MCUX_CSSL_FP_BRANCH_NEGATIVE_VALUE, condition)
705 
706 /**
707  * \def MCUX_CSSL_FP_BRANCH_TAKEN_NEGATIVE_IMPL1
708  * \brief Expectation implementation of an executed negative branch (without
709  *        condition).
710  * \ingroup csslFpCntBranch
711  *
712  * \see MCUX_CSSL_FP_BRANCH_TAKEN_NEGATIVE_IMPL
713  * \see MCUX_CSSL_FP_BRANCH_TAKEN_NEGATIVE_IMPL2
714  *
715  * \param id Identifier of the flow protected branch.
716  */
717 #define MCUX_CSSL_FP_BRANCH_TAKEN_NEGATIVE_IMPL1(id) \
718   MCUX_CSSL_FP_BRANCH_TAKEN_IMPL(id, MCUX_CSSL_FP_BRANCH_NEGATIVE_VALUE, true)
719 
720 /**
721  * \def MCUX_CSSL_FP_BRANCH_TAKEN_NEGATIVE_IMPL
722  * \brief Expectation implementation of an executed negative branch.
723  * \ingroup csslFpCntBranch
724  *
725  * Implemented as an overloaded macro to simplify the use of the API.
726  *
727  * \see MCUX_CSSL_FP_BRANCH_TAKEN_NEGATIVE_IMPL1
728  * \see MCUX_CSSL_FP_BRANCH_TAKEN_NEGATIVE_IMPL2
729  *
730  * \param id        Identifier of the flow protected branch.
731  * \param condition Optional, condition under which this branch is taken.
732  */
733 #define MCUX_CSSL_FP_BRANCH_TAKEN_NEGATIVE_IMPL(...) \
734   MCUX_CSSL_CPP_OVERLOADED2(MCUX_CSSL_FP_BRANCH_TAKEN_NEGATIVE_IMPL, __VA_ARGS__)
735 
736 
737 
738 /**
739  * \defgroup csslFpCntSwitch Switching flow protection
740  * \brief Support for flow protected switches.
741  * \ingroup mcuxCsslFlowProtection_SecureCounter
742  *
743  * \declaration{MCUX_CSSL_FP_SWITCH_DECL_IMPL}
744  * \event{MCUX_CSSL_FP_SWITCH_CASE_IMPL,MCUX_CSSL_FP_SWITCH_DEFAULT_IMPL}
745  * \expectation{MCUX_CSSL_FP_SWITCH_TAKEN_IMPL,MCUX_CSSL_FP_SWITCH_TAKEN_DEFAULT_IMPL}
746  */
747 
748 /**
749  * \def MCUX_CSSL_FP_SWITCH_ID
750  * \brief Generator for switch identifiers.
751  * \ingroup csslFpCntSwitch
752  *
753  * \param id Identifier for the flow protected switch.
754  * \return   Counter value for the given loop.
755  */
756 #define MCUX_CSSL_FP_SWITCH_ID(id) \
757   MCUX_CSSL_FP_ID_IMPL()
758 
759 /**
760  * \def MCUX_CSSL_FP_SWITCH_DECL_IMPL
761  * \brief Declaration implementation of a flow protected switch.
762  * \ingroup csslFpCntSwitch
763  *
764  * \param id Identifier for the switch that is flow protected.
765  */
766 #define MCUX_CSSL_FP_SWITCH_DECL_IMPL(id) \
767     MCUX_CSSL_FP_DECL_IMPL(SWITCH, id)
768 
769 /**
770  * \def MCUX_CSSL_FP_SWITCH_VALUE
771  * \brief Macro to get the value for a given switch.
772  * \ingroup csslFpCntSwitch
773  *
774  * \param id Identifier for the switch that is flow protected.
775  * \return   The counter value for the given switch \p id.
776  */
777 #define MCUX_CSSL_FP_SWITCH_VALUE(id) \
778   MCUX_CSSL_FP_DECL_NAME(SWITCH, id)
779 
780 /**
781  * \def MCUX_CSSL_FP_SWITCH_CASE_IMPLn
782  * \brief Case that is being handled from a switch.
783  * \ingroup csslFpCntSwitch
784  *
785  * \see MCUX_CSSL_FP_SWITCH_CASE_IMPL
786  * \see MCUX_CSSL_FP_SWITCH_CASE_IMPL2
787  *
788  * \param id     Identifier of the flow protected switch.
789  * \param case   Case value that is chosen in the switch.
790  * \param expect One or more (comma separated) declarations of expected code
791  *               flow behavior related to this event.
792  */
793 #define MCUX_CSSL_FP_SWITCH_CASE_IMPLn(id, case, ...) \
794   MCUX_CSSL_SC_ADD( \
795     (MCUX_CSSL_FP_SWITCH_VALUE(id) * (case)) \
796     - MCUX_CSSL_FP_EXPECTATIONS(__VA_ARGS__) \
797   )
798 
799 /**
800  * \def MCUX_CSSL_FP_SWITCH_CASE_IMPL2
801  * \brief Case that is being handled from a switch.
802  * \ingroup csslFpCntSwitch
803  *
804  * \see MCUX_CSSL_FP_SWITCH_CASE_IMPL
805  * \see MCUX_CSSL_FP_SWITCH_CASE_IMPLn
806  *
807  * \param id   Identifier of the flow protected switch.
808  * \param case Case value that is chosen in the switch.
809  */
810 #define MCUX_CSSL_FP_SWITCH_CASE_IMPL2(id, case) \
811   MCUX_CSSL_FP_SWITCH_CASE_IMPLn(id, case, 0u)
812 
813 /**
814  * \def MCUX_CSSL_FP_SWITCH_CASE_IMPL
815  * \brief Case that is being handled from a switch.
816  * \ingroup csslFpCntSwitch
817  *
818  * Implemented as an overloaded macro to simplify the use of the API.
819  *
820  * \see MCUX_CSSL_FP_SWITCH_CASE_IMPL2
821  * \see MCUX_CSSL_FP_SWITCH_CASE_IMPLn
822  *
823  * \param id     Identifier of the flow protected switch.
824  * \param case   Case value that is chosen in the switch.
825  * \param expect Zero or more (comma separated) declarations of expected code
826  *               flow behavior related to this event.
827  */
828 #define MCUX_CSSL_FP_SWITCH_CASE_IMPL(...) \
829   MCUX_CSSL_CPP_OVERLOADED2(MCUX_CSSL_FP_SWITCH_CASE_IMPL, __VA_ARGS__)
830 
831 /**
832  * \def MCUX_CSSL_FP_SWITCH_DEFAULT_VALUE
833  * \brief Value to use for default case.
834  * \ingroup csslFpCntSwitch
835  */
836 #define MCUX_CSSL_FP_SWITCH_DEFAULT_VALUE \
837   (0xDEFAu)
838 
839 /**
840  * \def MCUX_CSSL_FP_SWITCH_DEFAULT_IMPLn
841  * \brief Case that is being handled from a switch.
842  * \ingroup csslFpCntSwitch
843  *
844  * \see MCUX_CSSL_FP_SWITCH_DEFAULT_IMPL
845  * \see MCUX_CSSL_FP_SWITCH_DEFAULT_IMPL1
846  *
847  * \param id     Identifier of the flow protected switch.
848  * \param expect Zero or more (comma separated) declarations of expected code
849  *               flow behavior related to this event.
850  */
851 #define MCUX_CSSL_FP_SWITCH_DEFAULT_IMPLn(id, ...) \
852   MCUX_CSSL_FP_SWITCH_CASE_IMPLn( \
853     id, MCUX_CSSL_FP_SWITCH_DEFAULT_VALUE, __VA_ARGS__)
854 
855 /**
856  * \def MCUX_CSSL_FP_SWITCH_DEFAULT_IMPL1
857  * \brief Case that is being handled from a switch.
858  * \ingroup csslFpCntSwitch
859  *
860  * \see MCUX_CSSL_FP_SWITCH_DEFAULT_IMPL
861  * \see MCUX_CSSL_FP_SWITCH_DEFAULT_IMPLn
862  *
863  * \param id Identifier of the flow protected switch.
864  */
865 #define MCUX_CSSL_FP_SWITCH_DEFAULT_IMPL1(id) \
866   MCUX_CSSL_FP_SWITCH_DEFAULT_IMPLn(id, 0u) \
867 
868 /**
869  * \def MCUX_CSSL_FP_SWITCH_DEFAULT_IMPL
870  * \brief Case that is being handled from a switch.
871  * \ingroup csslFpCntSwitch
872  *
873  * Implemented as an overloaded macro to simplify the use of the API.
874  *
875  * \see MCUX_CSSL_FP_SWITCH_DEFAULT_IMPL1
876  * \see MCUX_CSSL_FP_SWITCH_DEFAULT_IMPLn
877  *
878  * \param id     Identifier of the flow protected switch.
879  * \param expect Zero or more (comma separated) declarations of expected code
880  *               flow behavior related to this event.
881  */
882 #define MCUX_CSSL_FP_SWITCH_DEFAULT_IMPL(...) \
883   MCUX_CSSL_CPP_OVERLOADED1(MCUX_CSSL_FP_SWITCH_DEFAULT_IMPL, __VA_ARGS__)
884 
885 /**
886  * \def MCUX_CSSL_FP_SWITCH_TAKEN_IMPL3
887  * \brief Expected that a specific case is handled from a switch.
888  * \ingroup csslFpCntSwitch
889  *
890  * \see MCUX_CSSL_FP_SWITCH_TAKEN_IMPL
891  * \see MCUX_CSSL_FP_SWITCH_TAKEN_IMPL2
892  *
893  * \param id        Identifier of the flow protected switch.
894  * \param case      Value of the case that is expected to be chosen in the
895  *                  switch.
896  * \param condition Optional, condition under which the \p case is taken.
897  */
898 #define MCUX_CSSL_FP_SWITCH_TAKEN_IMPL3(id, case, condition) \
899   MCUX_CSSL_FP_CONDITIONAL_IMPL(condition, \
900     MCUX_CSSL_FP_SWITCH_VALUE(id) * (case))
901 
902 /**
903  * \def MCUX_CSSL_FP_SWITCH_TAKEN_IMPL2
904  * \brief Expected that a specific case is handled from a switch.
905  * \ingroup csslFpCntSwitch
906  *
907  * \see MCUX_CSSL_FP_SWITCH_TAKEN_IMPL
908  * \see MCUX_CSSL_FP_SWITCH_TAKEN_IMPL3
909  *
910  * \param id   Identifier of the flow protected switch.
911  * \param case Value of the case that is expected to be chosen in the switch.
912  */
913 #define MCUX_CSSL_FP_SWITCH_TAKEN_IMPL2(id, case) \
914   MCUX_CSSL_FP_SWITCH_TAKEN_IMPL3(id, case, true)
915 
916 /**
917  * \def MCUX_CSSL_FP_SWITCH_TAKEN_IMPL
918  * \brief Expected that a specific case is handled from a switch.
919  * \ingroup csslFpCntSwitch
920  *
921  * Implemented as an overloaded macro to simplify the use of the API.
922  *
923  * \see MCUX_CSSL_FP_SWITCH_TAKEN_IMPL2
924  * \see MCUX_CSSL_FP_SWITCH_TAKEN_IMPL3
925  *
926  * \param id        Identifier of the flow protected switch.
927  * \param case      Value of the case that is expected to be chosen in the
928  *                  switch.
929  * \param condition Optional, condition under which the \p case is taken.
930  */
931 #define MCUX_CSSL_FP_SWITCH_TAKEN_IMPL(...) \
932   MCUX_CSSL_CPP_OVERLOADED3(MCUX_CSSL_FP_SWITCH_TAKEN_IMPL, __VA_ARGS__)
933 
934 /**
935  * \def MCUX_CSSL_FP_SWITCH_TAKEN_DEFAULT_IMPL2
936  * \brief Expected that default case is handled from a switch.
937  * \ingroup csslFpCntSwitch
938  *
939  * \see MCUX_CSSL_FP_SWITCH_TAKEN_DEFAULT_IMPL
940  * \see MCUX_CSSL_FP_SWITCH_TAKEN_DEFAULT_IMPL1
941  *
942  * \param id        Identifier of the flow protected switch.
943  * \param condition Condition under which the default case is taken.
944  */
945 #define MCUX_CSSL_FP_SWITCH_TAKEN_DEFAULT_IMPL2(id, condition) \
946   MCUX_CSSL_FP_SWITCH_TAKEN_IMPL3(id, MCUX_CSSL_FP_SWITCH_DEFAULT_VALUE, condition)
947 
948 /**
949  * \def MCUX_CSSL_FP_SWITCH_TAKEN_DEFAULT_IMPL1
950  * \brief Expected that default case is handled from a switch.
951  * \ingroup csslFpCntSwitch
952  *
953  * \see MCUX_CSSL_FP_SWITCH_TAKEN_DEFAULT_IMPL
954  * \see MCUX_CSSL_FP_SWITCH_TAKEN_DEFAULT_IMPL2
955  *
956  * \param id        Identifier of the flow protected switch.
957  */
958 #define MCUX_CSSL_FP_SWITCH_TAKEN_DEFAULT_IMPL1(id) \
959   MCUX_CSSL_FP_SWITCH_TAKEN_DEFAULT_IMPL2(id, true)
960 
961 /**
962  * \def MCUX_CSSL_FP_SWITCH_TAKEN_DEFAULT_IMPL
963  * \brief Expected that default case is handled from a switch.
964  * \ingroup csslFpCntSwitch
965  *
966  * Implemented as an overloaded macro to simplify the use of the API.
967  *
968  * \see MCUX_CSSL_FP_SWITCH_TAKEN_DEFAULT_IMPL1
969  * \see MCUX_CSSL_FP_SWITCH_TAKEN_DEFAULT_IMPL2
970  *
971  * \param id        Identifier of the flow protected switch.
972  * \param condition Optional, condition under which the default case is taken.
973  */
974 #define MCUX_CSSL_FP_SWITCH_TAKEN_DEFAULT_IMPL(...) \
975   MCUX_CSSL_CPP_OVERLOADED2(MCUX_CSSL_FP_SWITCH_TAKEN_DEFAULT_IMPL, __VA_ARGS__)
976 
977 
978 
979 #endif /* MCUX_CSSL_FLOW_PROTECTION_SECURE_COUNTER_COMMON_H_ */
980