Skip to content

Commit 61839d4

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 732e964 commit 61839d4

5 files changed

Lines changed: 303 additions & 85 deletions

File tree

benchmark/bench_modules/wh_bench_mod_cmac.c

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

2933
static 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
}

src/wh_client.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,9 @@ int wh_Client_KeyCacheDmaRequest(whClientContext* c, uint32_t flags,
13401340
const void* keyAddr, uint16_t keySz,
13411341
uint16_t keyId)
13421342
{
1343+
int ret;
13431344
whMessageKeystore_CacheDmaRequest* req = NULL;
1345+
uintptr_t keyAddrPtr = 0;
13441346

13451347
if (c == NULL || (labelSz > 0 && label == NULL)) {
13461348
return WH_ERROR_BADARGS;
@@ -1356,8 +1358,11 @@ int wh_Client_KeyCacheDmaRequest(whClientContext* c, uint32_t flags,
13561358
req->labelSz = labelSz;
13571359

13581360
/* Set up DMA buffer info */
1359-
req->key.addr = (uint64_t)((uintptr_t)keyAddr);
13601361
req->key.sz = keySz;
1362+
ret = wh_Client_DmaProcessClientAddress(
1363+
c, (uintptr_t)keyAddr, (void**)&keyAddrPtr, keySz,
1364+
WH_DMA_OPER_CLIENT_READ_PRE, (whDmaFlags){0});
1365+
req->key.addr = keyAddrPtr;
13611366

13621367
/* Copy label if provided, truncate if necessary */
13631368
if (labelSz > 0) {
@@ -1367,8 +1372,15 @@ int wh_Client_KeyCacheDmaRequest(whClientContext* c, uint32_t flags,
13671372
memcpy(req->label, label, labelSz);
13681373
}
13691374

1370-
return wh_Client_SendRequest(c, WH_MESSAGE_GROUP_KEY, WH_KEY_CACHE_DMA,
1371-
sizeof(*req), (uint8_t*)req);
1375+
if (ret == WH_ERROR_OK) {
1376+
ret = wh_Client_SendRequest(c, WH_MESSAGE_GROUP_KEY, WH_KEY_CACHE_DMA,
1377+
sizeof(*req), (uint8_t*)req);
1378+
}
1379+
1380+
(void)wh_Client_DmaProcessClientAddress(
1381+
c, (uintptr_t)keyAddr, (void**)&keyAddrPtr, keySz,
1382+
WH_DMA_OPER_CLIENT_READ_POST, (whDmaFlags){0});
1383+
return ret;
13721384
}
13731385

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

0 commit comments

Comments
 (0)