Skip to content

Commit 53d7586

Browse files
add cmac and start of other hash algos
fix for hash dma additions mldsa using DMA offset feature remove unused flag and add malloc to cmac benchmark add macro guards around DMA additions fix typo for state address with cmac run git-clang-format
1 parent 33ec360 commit 53d7586

File tree

5 files changed

+303
-85
lines changed

5 files changed

+303
-85
lines changed

benchmark/bench_modules/wh_bench_mod_cmac.c

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
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
}

src/wh_client.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,7 +1336,9 @@ int wh_Client_KeyCacheDmaRequest(whClientContext* c, uint32_t flags,
13361336
const void* keyAddr, uint16_t keySz,
13371337
uint16_t keyId)
13381338
{
1339+
int ret;
13391340
whMessageKeystore_CacheDmaRequest* req = NULL;
1341+
uintptr_t keyAddrPtr = 0;
13401342

13411343
if (c == NULL || (labelSz > 0 && label == NULL)) {
13421344
return WH_ERROR_BADARGS;
@@ -1352,8 +1354,11 @@ int wh_Client_KeyCacheDmaRequest(whClientContext* c, uint32_t flags,
13521354
req->labelSz = labelSz;
13531355

13541356
/* Set up DMA buffer info */
1355-
req->key.addr = (uint64_t)((uintptr_t)keyAddr);
13561357
req->key.sz = keySz;
1358+
ret = wh_Client_DmaProcessClientAddress(
1359+
c, (uintptr_t)keyAddr, (void**)&keyAddrPtr, keySz,
1360+
WH_DMA_OPER_CLIENT_READ_PRE, (whDmaFlags){0});
1361+
req->key.addr = keyAddrPtr;
13571362

13581363
/* Copy label if provided, truncate if necessary */
13591364
if (labelSz > 0) {
@@ -1363,8 +1368,15 @@ int wh_Client_KeyCacheDmaRequest(whClientContext* c, uint32_t flags,
13631368
memcpy(req->label, label, labelSz);
13641369
}
13651370

1366-
return wh_Client_SendRequest(c, WH_MESSAGE_GROUP_KEY, WH_KEY_CACHE_DMA,
1367-
sizeof(*req), (uint8_t*)req);
1371+
if (ret == WH_ERROR_OK) {
1372+
ret = wh_Client_SendRequest(c, WH_MESSAGE_GROUP_KEY, WH_KEY_CACHE_DMA,
1373+
sizeof(*req), (uint8_t*)req);
1374+
}
1375+
1376+
(void)wh_Client_DmaProcessClientAddress(
1377+
c, (uintptr_t)keyAddr, (void**)&keyAddrPtr, keySz,
1378+
WH_DMA_OPER_CLIENT_READ_POST, (whDmaFlags){0});
1379+
return ret;
13681380
}
13691381

13701382
int wh_Client_KeyCacheDmaResponse(whClientContext* c, uint16_t* keyId)

0 commit comments

Comments
 (0)