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_TEST_POSIX */
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,32 @@ 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 defined(WOLFHSM_CFG_TEST_POSIX )
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+ XFREE (in , heap , DYNAMIC_TYPE_TMP_BUFFER );
84+ return WH_ERROR_NOSPACE ;
85+ }
86+ }
87+ else {
88+ in = WH_BENCH_DMA_BUFFER ;
89+ }
90+ #endif /* WOLFHSM_CFG_TEST_POSIX */
6491 }
6592 else
6693#endif
@@ -101,7 +128,7 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
101128 benchStartRet = wh_Bench_StartOp (ctx , id );
102129 /* Oneshot CMAC through wolfCrypt API will always be most performant
103130 * implementation */
104- ret = wc_AesCmacGenerate_ex (cmac , tag , & outLen , in , inLen , key , keyLen ,
131+ ret = wc_AesCmacGenerate_ex (cmac , out , & outLen , in , inLen , key , keyLen ,
105132 NULL , devId );
106133 benchStopRet = wh_Bench_StopOp (ctx , id );
107134
@@ -131,6 +158,18 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
131158 ret = evictRet ;
132159 }
133160 }
161+ #if defined(WOLFHSM_CFG_DMA )
162+ #if defined(WOLFHSM_CFG_TEST_POSIX )
163+ if (devId == WH_DEV_ID_DMA &&
164+ ctx -> transportType == WH_BENCH_TRANSPORT_POSIX_DMA ) {
165+ /* if static memory was used with DMA then use XFREE */
166+ void * heap =
167+ posixTransportShm_GetDmaHeap (client -> comm -> transport_context );
168+ XFREE (in , heap , DYNAMIC_TYPE_TMP_BUFFER );
169+ XFREE (out , heap , DYNAMIC_TYPE_TMP_BUFFER );
170+ }
171+ #endif /* WOLFHSM_CFG_TEST_POSIX */
172+ #endif
134173 (void )wc_CmacFree (cmac );
135174 return ret ;
136175}
0 commit comments