MCUX CLNS
MCUX Crypto Library Normal Secure
Loading...
Searching...
No Matches
mcuxClExample_Session_Helper.h
1/*--------------------------------------------------------------------------*/
2/* Copyright 2022-2026 NXP */
3/* */
4/* SPDX-License-Identifier: BSD-3-Clause */
5/* */
6/* Redistribution and use in source and binary forms, with or without */
7/* modification, are permitted provided that the following conditions are */
8/* met: */
9/* */
10/* 1. Redistributions of source code must retain the above copyright */
11/* notice, this list of conditions and the following disclaimer. */
12/* */
13/* 2. Redistributions in binary form must reproduce the above copyright */
14/* notice, this list of conditions and the following disclaimer in the */
15/* documentation and/or other materials provided with the distribution. */
16/* */
17/* 3. Neither the name of the copyright holder nor the names of its */
18/* contributors may be used to endorse or promote products derived from */
19/* this software without specific prior written permission. */
20/* */
21/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS */
22/* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED */
23/* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A */
24/* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
25/* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
26/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
27/* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR */
28/* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
29/* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
30/* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
31/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
32/*--------------------------------------------------------------------------*/
33
34#ifndef MCUXCLEXAMPLE_SESSION_HELPER_H_
35#define MCUXCLEXAMPLE_SESSION_HELPER_H_
36
37#include <mcuxClConfig.h> // Exported features flags header
38#include <mcuxClCore_Platform.h>
39#include <mcuxClCore_Examples.h>
40#include <mcuxClSession.h>
41#include <mcuxClResource.h>
44#include <platform_specific_headers.h>
45
52
53/* Macros to make sure some memory is always allocated to avoid issues */
54#define MCUXCLEXAMPLE_ALLOCATE_CPUWA(cpuWaLength) \
55 MCUX_CSSL_ANALYSIS_START_SUPPRESS_CONTROLLING_EXPRESSION_IS_INVARIANT("Constant values are allowed as an argument to macro function") \
56 ((((sizeof(uint32_t)) <= (cpuWaLength))?(cpuWaLength):sizeof(uint32_t)) / (sizeof(uint32_t))) \
57 MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_CONTROLLING_EXPRESSION_IS_INVARIANT()
58#define MCUXCLEXAMPLE_ALLOCATE_PKCWA(pkcWaLength) \
59 MCUX_CSSL_ANALYSIS_START_SUPPRESS_CONTROLLING_EXPRESSION_IS_INVARIANT("Constant values are allowed as an argument to macro function") \
60 ((((sizeof(uint32_t)) <= (pkcWaLength))?(pkcWaLength):sizeof(uint32_t)) / (sizeof(uint32_t))) \
61 MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_CONTROLLING_EXPRESSION_IS_INVARIANT()
62
63#define MCUXCLEXAMPLE_MAX_WA(a, b) \
64 MCUX_CSSL_ANALYSIS_START_SUPPRESS_CONTROLLING_EXPRESSION_IS_INVARIANT("Constant values are allowed as an argument to macro function") \
65 (((a) > (b)) ? (a) : (b)) \
66 MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_CONTROLLING_EXPRESSION_IS_INVARIANT()
67
68#include <mcuxClResource.h>
69
75MCUX_CSSL_FP_FUNCTION_DEF(mcuxClExample_Session_InitAndSetResourceCtx)
76static inline bool mcuxClExample_Session_InitAndSetResourceCtx(
78 mcuxClResource_Context_t * pResourceCtx)
79{
81 /* mcuxClResource_Context_t * pResourceCtx: */ pResourceCtx,
82 MCUX_CSSL_ANALYSIS_START_SUPPRESS_NULL_POINTER_CONSTANT("NULL is used in code")
83 /* mcuxClResource_MutexAcquire_Callback_t pMutexAcquire: */ NULL,
84 /* mcuxClResource_MutexRelease_Callback_t pMutexRelease: */ NULL
85 MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_NULL_POINTER_CONSTANT()
86 ));
88 {
89 return MCUXCLEXAMPLE_STATUS_ERROR;
90 }
92
94 /* mcuxClSession_Handle_t session: */ pSession,
95 /* mcuxClResource_Context_t * pResourceCtx: */ pResourceCtx));
97 {
98 return MCUXCLEXAMPLE_STATUS_ERROR;
99 }
101
102 return MCUXCLEXAMPLE_STATUS_OK;
103}
104
105#define MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_SESSION(pSession, cpuWaLength, pkcWaLength) \
106 uint32_t cpuWaBuffer[MCUXCLEXAMPLE_ALLOCATE_CPUWA(cpuWaLength)]; \
107 MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(si_status, token, mcuxClSession_init( \
108 /* mcuxClSession_Handle_t session: */ pSession, \
109 /* uint32_t * const cpuWaBuffer: */ cpuWaBuffer, \
110 /* uint32_t cpuWaSize: */ cpuWaLength, \
111 MCUX_CSSL_ANALYSIS_START_SUPPRESS_DISCARD_CONST_QUALIFIER() \
112 /* uint32_t * const pkcWaBuffer: */ (uint32_t *) PKC_RAM_ADDR, \
113 MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_DISCARD_CONST_QUALIFIER() \
114 /* uint32_t pkcWaSize: */ pkcWaLength \
115 )); \
116 /* mcuxClSession_init is a flow-protected function: Check the protection token and the return value */ \
117 if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClSession_init) != token) || (MCUXCLSESSION_STATUS_OK != si_status)) \
118 { \
119 return MCUXCLEXAMPLE_STATUS_ERROR; \
120 } \
121 MCUX_CSSL_FP_FUNCTION_CALL_END(); \
122 uint32_t resCtx[MCUXCLRESOURCE_CONTEXT_SIZE/sizeof(uint32_t)]; \
123 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")\
124 if (!mcuxClExample_Session_InitAndSetResourceCtx(pSession, (mcuxClResource_Context_t *)resCtx)) \
125 MCUX_CSSL_ANALYSIS_COVERITY_STOP_DEVIATE(MISRA_C_2012_Rule_10_3) \
126 { \
127 return MCUXCLEXAMPLE_STATUS_ERROR; \
128 }
129
130/* For non-blocking, resource context needs to be global to be used for interrupt/callback handlers,
131 until such handling is incorporated for all examples. */
132#define MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_SESSION_NONBLOCKING(pSession, cpuWaLength, pkcWaLength) \
133 uint32_t cpuWaBuffer[MCUXCLEXAMPLE_ALLOCATE_CPUWA(cpuWaLength)]; \
134 MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(si_status, token, mcuxClSession_init( \
135 /* mcuxClSession_Handle_t session: */ pSession, \
136 /* uint32_t * const cpuWaBuffer: */ cpuWaBuffer, \
137 /* uint32_t cpuWaSize: */ cpuWaLength, \
138 MCUX_CSSL_ANALYSIS_START_SUPPRESS_DISCARD_CONST_QUALIFIER() \
139 /* uint32_t * const pkcWaBuffer: */ (uint32_t *) PKC_RAM_ADDR, \
140 MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_DISCARD_CONST_QUALIFIER() \
141 /* uint32_t pkcWaSize: */ pkcWaLength \
142 )); \
143 /* mcuxClSession_init is a flow-protected function: Check the protection token and the return value */ \
144 if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClSession_init) != token) || (MCUXCLSESSION_STATUS_OK != si_status)) \
145 { \
146 return MCUXCLEXAMPLE_STATUS_ERROR; \
147 } \
148 MCUX_CSSL_FP_FUNCTION_CALL_END();
149
154MCUX_CSSL_FP_FUNCTION_DEF(mcuxClExample_Session_Clean)
155static inline bool mcuxClExample_Session_Clean(mcuxClSession_Handle_t pSession)
156{
157 MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(cleanup_result, cleanup_token, mcuxClSession_cleanup(pSession));
158
159 if(MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClSession_cleanup) != cleanup_token || MCUXCLSESSION_STATUS_OK != cleanup_result)
160 {
161 return MCUXCLEXAMPLE_STATUS_ERROR;
162 }
163
165
166 return MCUXCLEXAMPLE_STATUS_OK;
167}
168
169#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:67
#define MCUXCLRESOURCE_STATUS_OK
Resource operation successful.
Definition mcuxClResource_Types.h:102
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:59
mcuxClSession_Descriptor_t *const mcuxClSession_Handle_t
Type for mcuxClSession Handle.
Definition mcuxClSession_Types.h:118
#define MCUX_CSSL_FP_FUNCTION_DEF(...)
Definition of a flow protected function.
Definition mcuxCsslFlowProtection.h:220
#define MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(...)
Call a flow protected function and check the protection token.
Definition mcuxCsslFlowProtection.h:643
#define MCUX_CSSL_FP_FUNCTION_CALLED(...)
Expectation of a called function.
Definition mcuxCsslFlowProtection.h:797
#define MCUX_CSSL_FP_FUNCTION_CALL_END(...)
End a function call section started by MCUX_CSSL_FP_FUNCTION_CALL_BEGIN.
Definition mcuxCsslFlowProtection.h:678