MCUX CLNS
MCUX Crypto Library Normal Secure
Loading...
Searching...
No Matches
mcuxClExample_Session_Helper.h
1/*--------------------------------------------------------------------------*/
2/* Copyright 2022-2026 NXP */
3/* */
4/* NXP Confidential and Proprietary. This software is owned or controlled */
5/* by NXP and may only be used strictly in accordance with the applicable */
6/* license terms. By expressly accepting such terms or by downloading, */
7/* installing, activating and/or otherwise using the software, you are */
8/* agreeing that you have read, and that you agree to comply with and are */
9/* bound by, such license terms. If you do not agree to be bound by the */
10/* applicable license terms, then you may not retain, install, activate or */
11/* otherwise use the software. */
12/*--------------------------------------------------------------------------*/
13
14#ifndef MCUXCLEXAMPLE_SESSION_HELPER_H_
15#define MCUXCLEXAMPLE_SESSION_HELPER_H_
16
17#include <mcuxClConfig.h> // Exported features flags header
18#include <mcuxClCore_Platform.h>
19#include <mcuxClCore_Examples.h>
20#include <mcuxClSession.h>
21#include <mcuxClResource.h>
24#include <platform_specific_headers.h>
25
32
33/* Macros to make sure some memory is always allocated to avoid issues */
34#define MCUXCLEXAMPLE_ALLOCATE_CPUWA(cpuWaLength) \
35 MCUX_CSSL_ANALYSIS_START_SUPPRESS_CONTROLLING_EXPRESSION_IS_INVARIANT("Constant values are allowed as an argument to macro function") \
36 ((((sizeof(uint32_t)) <= (cpuWaLength))?(cpuWaLength):sizeof(uint32_t)) / (sizeof(uint32_t))) \
37 MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_CONTROLLING_EXPRESSION_IS_INVARIANT()
38#define MCUXCLEXAMPLE_ALLOCATE_PKCWA(pkcWaLength) \
39 MCUX_CSSL_ANALYSIS_START_SUPPRESS_CONTROLLING_EXPRESSION_IS_INVARIANT("Constant values are allowed as an argument to macro function") \
40 ((((sizeof(uint32_t)) <= (pkcWaLength))?(pkcWaLength):sizeof(uint32_t)) / (sizeof(uint32_t))) \
41 MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_CONTROLLING_EXPRESSION_IS_INVARIANT()
42
43#define MCUXCLEXAMPLE_MAX_WA(a, b) \
44 MCUX_CSSL_ANALYSIS_START_SUPPRESS_CONTROLLING_EXPRESSION_IS_INVARIANT("Constant values are allowed as an argument to macro function") \
45 (((a) > (b)) ? (a) : (b)) \
46 MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_CONTROLLING_EXPRESSION_IS_INVARIANT()
47
48#include <mcuxClResource.h>
49
55MCUX_CSSL_FP_FUNCTION_DEF(mcuxClExample_Session_InitAndSetResourceCtx)
56static inline bool mcuxClExample_Session_InitAndSetResourceCtx(
58 mcuxClResource_Context_t * pResourceCtx)
59{
61 /* mcuxClResource_Context_t * pResourceCtx: */ pResourceCtx,
62 MCUX_CSSL_ANALYSIS_START_SUPPRESS_NULL_POINTER_CONSTANT("NULL is used in code")
63 /* mcuxClResource_MutexAcquire_Callback_t pMutexAcquire: */ NULL,
64 /* mcuxClResource_MutexRelease_Callback_t pMutexRelease: */ NULL
65 MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_NULL_POINTER_CONSTANT()
66 ));
68 {
69 return MCUXCLEXAMPLE_STATUS_ERROR;
70 }
72
74 /* mcuxClSession_Handle_t session: */ pSession,
75 /* mcuxClResource_Context_t * pResourceCtx: */ pResourceCtx));
77 {
78 return MCUXCLEXAMPLE_STATUS_ERROR;
79 }
81
82 return MCUXCLEXAMPLE_STATUS_OK;
83}
84
85#define MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_SESSION(pSession, cpuWaLength, pkcWaLength) \
86 uint32_t cpuWaBuffer[MCUXCLEXAMPLE_ALLOCATE_CPUWA(cpuWaLength)]; \
87 MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(si_status, token, mcuxClSession_init( \
88 /* mcuxClSession_Handle_t session: */ pSession, \
89 /* uint32_t * const cpuWaBuffer: */ cpuWaBuffer, \
90 /* uint32_t cpuWaSize: */ cpuWaLength, \
91 MCUX_CSSL_ANALYSIS_START_SUPPRESS_DISCARD_CONST_QUALIFIER() \
92 /* uint32_t * const pkcWaBuffer: */ (uint32_t *) PKC_RAM_ADDR, \
93 MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_DISCARD_CONST_QUALIFIER() \
94 /* uint32_t pkcWaSize: */ pkcWaLength \
95 )); \
96 /* mcuxClSession_init is a flow-protected function: Check the protection token and the return value */ \
97 if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClSession_init) != token) || (MCUXCLSESSION_STATUS_OK != si_status)) \
98 { \
99 return MCUXCLEXAMPLE_STATUS_ERROR; \
100 } \
101 MCUX_CSSL_FP_FUNCTION_CALL_END(); \
102 uint32_t resCtx[MCUXCLRESOURCE_CONTEXT_SIZE/sizeof(uint32_t)]; \
103 MCUX_CSSL_ANALYSIS_COVERITY_START_DEVIATE(MISRA_C_2012_Rule_10_3, "Implicit cast between int and bool in examples is allowed for readability purposes")\
104 if (!mcuxClExample_Session_InitAndSetResourceCtx(pSession, (mcuxClResource_Context_t *)resCtx)) \
105 MCUX_CSSL_ANALYSIS_COVERITY_STOP_DEVIATE(MISRA_C_2012_Rule_10_3) \
106 { \
107 return MCUXCLEXAMPLE_STATUS_ERROR; \
108 }
109
110/* For non-blocking, resource context needs to be global to be used for interrupt/callback handlers,
111 until such handling is incorporated for all examples. */
112#define MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_SESSION_NONBLOCKING(pSession, cpuWaLength, pkcWaLength) \
113 uint32_t cpuWaBuffer[MCUXCLEXAMPLE_ALLOCATE_CPUWA(cpuWaLength)]; \
114 MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(si_status, token, mcuxClSession_init( \
115 /* mcuxClSession_Handle_t session: */ pSession, \
116 /* uint32_t * const cpuWaBuffer: */ cpuWaBuffer, \
117 /* uint32_t cpuWaSize: */ cpuWaLength, \
118 MCUX_CSSL_ANALYSIS_START_SUPPRESS_DISCARD_CONST_QUALIFIER() \
119 /* uint32_t * const pkcWaBuffer: */ (uint32_t *) PKC_RAM_ADDR, \
120 MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_DISCARD_CONST_QUALIFIER() \
121 /* uint32_t pkcWaSize: */ pkcWaLength \
122 )); \
123 /* mcuxClSession_init is a flow-protected function: Check the protection token and the return value */ \
124 if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClSession_init) != token) || (MCUXCLSESSION_STATUS_OK != si_status)) \
125 { \
126 return MCUXCLEXAMPLE_STATUS_ERROR; \
127 } \
128 MCUX_CSSL_FP_FUNCTION_CALL_END();
129
134MCUX_CSSL_FP_FUNCTION_DEF(mcuxClExample_Session_Clean)
135static inline bool mcuxClExample_Session_Clean(mcuxClSession_Handle_t pSession)
136{
137 MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(cleanup_result, cleanup_token, mcuxClSession_cleanup(pSession));
138
139 if(MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClSession_cleanup) != cleanup_token || MCUXCLSESSION_STATUS_OK != cleanup_result)
140 {
141 return MCUXCLEXAMPLE_STATUS_ERROR;
142 }
143
145
146 return MCUXCLEXAMPLE_STATUS_OK;
147}
148
149#endif /* MCUXCLEXAMPLE_SESSION_HELPER_H_ */
Definition of function identifiers for the flow protection mechanism.
Top-level include file for the mcuxClResource component.
Top-level include file for the mcuxClSession component.
Provides the API for the CSSL flow protection mechanism.
mcuxClResource_Status_t mcuxClResource_init(mcuxClResource_Context_t *pResourceCtx, mcuxClResource_MutexAcquire_Callback_t pMutexAcquire, mcuxClResource_MutexRelease_Callback_t pMutexRelease)
Resource context initialization function.
struct mcuxClResource_Context mcuxClResource_Context_t
Resource context type.
Definition mcuxClResource_Types.h:47
#define MCUXCLRESOURCE_STATUS_OK
Resource operation successful.
Definition mcuxClResource_Types.h:82
mcuxClSession_Status_t mcuxClSession_cleanup(mcuxClSession_Handle_t pSession)
Clean up a Crypto Library session.
mcuxClSession_Status_t mcuxClSession_setResource(mcuxClSession_Handle_t session, mcuxClResource_Context_t *pResourceCtx)
Set the resource context in a Crypto Library session.
#define MCUXCLSESSION_STATUS_OK
Session operation successful.
Definition mcuxClSession_Types.h:39
mcuxClSession_Descriptor_t *const mcuxClSession_Handle_t
Type for mcuxClSession Handle.
Definition mcuxClSession_Types.h:98
#define MCUX_CSSL_FP_FUNCTION_DEF(...)
Definition of a flow protected function.
Definition mcuxCsslFlowProtection.h:200
#define MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(...)
Call a flow protected function and check the protection token.
Definition mcuxCsslFlowProtection.h:623
#define MCUX_CSSL_FP_FUNCTION_CALLED(...)
Expectation of a called function.
Definition mcuxCsslFlowProtection.h:777
#define MCUX_CSSL_FP_FUNCTION_CALL_END(...)
End a function call section started by MCUX_CSSL_FP_FUNCTION_CALL_BEGIN.
Definition mcuxCsslFlowProtection.h:658