2424#if !defined(WOLFHSM_CFG_NO_CRYPTO ) && defined(WOLFHSM_CFG_BENCH_ENABLE )
2525#include "wolfssl/wolfcrypt/cmac.h"
2626
27+ #if defined(WOLFHSM_CFG_DMA ) && defined(WOLFHSM_CFG_TEST_POSIX )
28+ #include "port/posix/posix_transport_shm.h"
29+ #endif /* WOLFHSM_CFG_DMA && WOLFHSM_CFG_POSIX_TRANSPORT */
30+
2731#if defined(WOLFSSL_CMAC ) && !defined(NO_AES ) && defined(WOLFSSL_AES_DIRECT )
2832
2933static const uint8_t key128 [] = {0x2b , 0x7e , 0x15 , 0x16 , 0x28 , 0xae ,
@@ -44,10 +48,11 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
4448 whKeyId keyId = WH_KEYID_ERASED ;
4549 Cmac cmac [1 ];
4650 char keyLabel [] = "baby's first key" ;
47- byte tag [16 ];
51+ byte tag [WC_CMAC_TAG_MAX_SZ ];
4852 int i ;
4953 uint8_t * in = NULL ;
5054 size_t inLen ;
55+ uint8_t * out = NULL ;
5156
5257 /* cache the key on the HSM */
5358 ret = wh_Client_KeyCache (client , 0 , (uint8_t * )keyLabel , sizeof (keyLabel ),
@@ -57,10 +62,29 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
5762 return ret ;
5863 }
5964
65+ out = tag ; /* default to using tag buffer on the stack */
6066#if defined(WOLFHSM_CFG_DMA )
6167 if (devId == WH_DEV_ID_DMA ) {
62- in = WH_BENCH_DMA_BUFFER ;
6368 inLen = WOLFHSM_CFG_BENCH_DMA_BUFFER_SIZE ;
69+ if (ctx -> transportType == WH_BENCH_TRANSPORT_POSIX_DMA ) {
70+ /* if static memory was used with DMA then use XMALLOC */
71+ void * heap =
72+ posixTransportShm_GetDmaHeap (client -> comm -> transport_context );
73+ in = (uint8_t * )XMALLOC (inLen , heap , DYNAMIC_TYPE_TMP_BUFFER );
74+ if (in == NULL ) {
75+ WH_BENCH_PRINTF ("Failed to allocate memory for DMA\n" );
76+ return WH_ERROR_NOSPACE ;
77+ }
78+ out = (uint8_t * )XMALLOC (WC_CMAC_TAG_MAX_SZ , heap ,
79+ DYNAMIC_TYPE_TMP_BUFFER );
80+ if (out == NULL ) {
81+ WH_BENCH_PRINTF ("Failed to allocate memory for DMA\n" );
82+ return WH_ERROR_NOSPACE ;
83+ }
84+ }
85+ else {
86+ in = WH_BENCH_DMA_BUFFER ;
87+ }
6488 }
6589 else
6690#endif
@@ -101,7 +125,7 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
101125 benchStartRet = wh_Bench_StartOp (ctx , id );
102126 /* Oneshot CMAC through wolfCrypt API will always be most performant
103127 * implementation */
104- ret = wc_AesCmacGenerate_ex (cmac , tag , & outLen , in , inLen , key , keyLen ,
128+ ret = wc_AesCmacGenerate_ex (cmac , out , & outLen , in , inLen , key , keyLen ,
105129 NULL , devId );
106130 benchStopRet = wh_Bench_StopOp (ctx , id );
107131
@@ -131,6 +155,16 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
131155 ret = evictRet ;
132156 }
133157 }
158+ #if defined(WOLFHSM_CFG_DMA )
159+ if (devId == WH_DEV_ID_DMA &&
160+ ctx -> transportType == WH_BENCH_TRANSPORT_POSIX_DMA ) {
161+ /* if static memory was used with DMA then use XFREE */
162+ void * heap =
163+ posixTransportShm_GetDmaHeap (client -> comm -> transport_context );
164+ XFREE (in , heap , DYNAMIC_TYPE_TMP_BUFFER );
165+ XFREE (out , heap , DYNAMIC_TYPE_TMP_BUFFER );
166+ }
167+ #endif
134168 (void )wc_CmacFree (cmac );
135169 return ret ;
136170}
0 commit comments