MCUX CLNS
MCUX Crypto Library Normal Secure
mcuxClExample_Session_Helper.h
1 /*--------------------------------------------------------------------------*/
2 /* Copyright 2022-2023 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 #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 <mcuxClSession.h>
20 #include <mcuxCsslFlowProtection.h>
22 #include <platform_specific_headers.h>
23 
31 /* TODO: CLNS-5886 [DEV][Session][Example] enable size checking when allocating buffers and update example macros */
32 #if 0
33 #define MCUXCLEXAMPLE_ALLOCATE_CPUWA(cpuWaLength) ((cpuWaLength?cpuWaLength:(sizeof(uint32_t))) / (sizeof(uint32_t))) // always allocate a minimum size buffer to avoid issues
34 #define MCUXCLEXAMPLE_ALLOCATE_PKCWA(pkcWaLength) ((pkcWaLength?pkcWaLength:(sizeof(uint32_t))) / (sizeof(uint32_t))) // always allocate a minimum size buffer to avoid issues
35 #else
36 #define MCUXCLEXAMPLE_ALLOCATE_CPUWA(cpuWaLength) (cpuWaLength?cpuWaLength:1u) // always allocate a minimum size buffer to avoid issues
37 #define MCUXCLEXAMPLE_ALLOCATE_PKCWA(pkcWaLength) (pkcWaLength?pkcWaLength:1u) // always allocate a minimum size buffer to avoid issues
38 #endif
39 
40 #define MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_SESSION(pSession, cpuWaLength, pkcWaLength) \
41  uint32_t cpuWaBuffer[MCUXCLEXAMPLE_ALLOCATE_CPUWA(cpuWaLength)]; \
42  MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(si_status, token, mcuxClSession_init( \
43  /* mcuxClSession_Handle_t session: */ pSession, \
44  /* uint32_t * const cpuWaBuffer: */ cpuWaBuffer, \
45  /* uint32_t cpuWaSize: */ cpuWaLength, \
46  /* uint32_t * const pkcWaBuffer: */ (uint32_t *) PKC_RAM_ADDR, \
47  /* uint32_t pkcWaSize: */ pkcWaLength \
48  )); \
49  /* mcuxClSession_init is a flow-protected function: Check the protection token and the return value */ \
50  if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClSession_init) != token) || (MCUXCLSESSION_STATUS_OK != si_status)) \
51  { \
52  return false; \
53  } \
54  MCUX_CSSL_FP_FUNCTION_CALL_END();
55 
60 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClExample_Session_Clean)
61 static inline bool mcuxClExample_Session_Clean(mcuxClSession_Handle_t pSession)
62 {
63  MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(cleanup_result, cleanup_token, mcuxClSession_cleanup(pSession));
64 
65  if(MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClSession_cleanup) != cleanup_token || MCUXCLSESSION_STATUS_OK != cleanup_result)
66  {
67  return false;
68  }
69 
71 
72  MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(destroy_result, destroy_token, mcuxClSession_destroy(pSession));
73 
74  if(MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClSession_destroy) != destroy_token || MCUXCLSESSION_STATUS_OK != destroy_result)
75  {
76  return false;
77  }
78 
80  return true;
81 }
82 
83 #endif /* MCUXCLEXAMPLE_SESSION_HELPER_H_ */
#define MCUX_CSSL_FP_FUNCTION_CALLED(...)
Expectation of a called function.
Definition: mcuxCsslFlowProtection.h:730
#define MCUXCLSESSION_STATUS_OK
Session operation successful.
Definition: mcuxClSession_Types.h:44
Top-level include file for the mcuxClSession component.
#define MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(...)
Call a flow protected function and check the protection token.
Definition: mcuxCsslFlowProtection.h:576
mcuxClSession_Status_t mcuxClSession_cleanup(mcuxClSession_Handle_t pSession)
Clean up a Crypto Library session.
#define MCUX_CSSL_FP_FUNCTION_CALL_END(...)
End a function call section started by MCUX_CSSL_FP_FUNCTION_CALL_BEGIN.
Definition: mcuxCsslFlowProtection.h:611
#define MCUX_CSSL_FP_FUNCTION_DEF(...)
Definition of a flow protected function.
Definition: mcuxCsslFlowProtection.h:159
Definition of function identifiers for the flow protection mechanism.
Provides the API for the CSSL flow protection mechanism.
mcuxClSession_Descriptor_t *const mcuxClSession_Handle_t
Type for mcuxClSession Handle.
Definition: mcuxClSession_Types.h:133
mcuxClSession_Status_t mcuxClSession_destroy(mcuxClSession_Handle_t pSession)
Destroy a Crypto Library session.