2323
2424#include "wolfssl/wolfcrypt/cmac.h"
2525
26+ #if defined(WOLFHSM_CFG_DMA ) && defined(WOLFHSM_CFG_TEST_POSIX )
27+ #include "port/posix/posix_transport_shm.h"
28+ #endif /* WOLFHSM_CFG_DMA && WOLFHSM_CFG_POSIX_TRANSPORT */
29+
2630#if defined(WOLFHSM_CFG_BENCH_ENABLE )
2731
2832#if defined(WOLFSSL_CMAC ) && !defined(NO_AES ) && defined(WOLFSSL_AES_DIRECT )
@@ -45,10 +49,11 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
4549 whKeyId keyId = WH_KEYID_ERASED ;
4650 Cmac cmac [1 ];
4751 char keyLabel [] = "baby's first key" ;
48- byte tag [16 ];
52+ byte tag [WC_CMAC_TAG_MAX_SZ ];
4953 int i ;
5054 uint8_t * in = NULL ;
5155 size_t inLen ;
56+ uint8_t * out = NULL ;
5257
5358 /* cache the key on the HSM */
5459 ret = wh_Client_KeyCache (client , 0 , (uint8_t * )keyLabel , sizeof (keyLabel ),
@@ -58,10 +63,29 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
5863 return ret ;
5964 }
6065
66+ out = tag ; /* default to using tag buffer on the stack */
6167#if defined(WOLFHSM_CFG_DMA )
6268 if (devId == WH_DEV_ID_DMA ) {
63- in = WH_BENCH_DMA_BUFFER ;
6469 inLen = WOLFHSM_CFG_BENCH_DMA_BUFFER_SIZE ;
70+ if (ctx -> transportType == WH_BENCH_TRANSPORT_POSIX_DMA ) {
71+ /* if static memory was used with DMA then use XMALLOC */
72+ void * heap =
73+ posixTransportShm_GetDmaHeap (client -> comm -> transport_context );
74+ in = (uint8_t * )XMALLOC (inLen , heap , DYNAMIC_TYPE_TMP_BUFFER );
75+ if (in == NULL ) {
76+ WH_BENCH_PRINTF ("Failed to allocate memory for DMA\n" );
77+ return WH_ERROR_NOSPACE ;
78+ }
79+ out = (uint8_t * )XMALLOC (WC_CMAC_TAG_MAX_SZ , heap ,
80+ DYNAMIC_TYPE_TMP_BUFFER );
81+ if (out == NULL ) {
82+ WH_BENCH_PRINTF ("Failed to allocate memory for DMA\n" );
83+ return WH_ERROR_NOSPACE ;
84+ }
85+ }
86+ else {
87+ in = WH_BENCH_DMA_BUFFER ;
88+ }
6589 }
6690 else
6791#endif
@@ -102,7 +126,7 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
102126 benchStartRet = wh_Bench_StartOp (ctx , id );
103127 /* Oneshot CMAC through wolfCrypt API will always be most performant
104128 * implementation */
105- ret = wc_AesCmacGenerate_ex (cmac , tag , & outLen , in , inLen , key , keyLen ,
129+ ret = wc_AesCmacGenerate_ex (cmac , out , & outLen , in , inLen , key , keyLen ,
106130 NULL , devId );
107131 benchStopRet = wh_Bench_StopOp (ctx , id );
108132
@@ -132,6 +156,16 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
132156 ret = evictRet ;
133157 }
134158 }
159+ #if defined(WOLFHSM_CFG_DMA )
160+ if (devId == WH_DEV_ID_DMA &&
161+ ctx -> transportType == WH_BENCH_TRANSPORT_POSIX_DMA ) {
162+ /* if static memory was used with DMA then use XFREE */
163+ void * heap =
164+ posixTransportShm_GetDmaHeap (client -> comm -> transport_context );
165+ XFREE (in , heap , DYNAMIC_TYPE_TMP_BUFFER );
166+ XFREE (out , heap , DYNAMIC_TYPE_TMP_BUFFER );
167+ }
168+ #endif
135169 (void )wc_CmacFree (cmac );
136170 return ret ;
137171}
0 commit comments