Skip to content

Commit d3f43c9

Browse files
Expanding static memory DMA offset feature (#191)
* 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 * add return value checks, free in benchmark buffer on error, add macro guards * fix typo and add fix return value checks * fix flag for read/write with DMA operations
1 parent 398de7f commit d3f43c9

File tree

6 files changed

+413
-118
lines changed

6 files changed

+413
-118
lines changed

benchmark/bench_modules/wh_bench_mod_cmac.c

Lines changed: 42 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_TEST_POSIX */
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,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
}

benchmark/bench_modules/wh_bench_mod_sha2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ int _benchSha256(whClientContext* client, whBenchOpContext* ctx, int id,
5252
#if defined(WOLFHSM_CFG_DMA)
5353
if (devId == WH_DEV_ID_DMA) {
5454
inLen = WOLFHSM_CFG_BENCH_DMA_BUFFER_SIZE;
55-
5655
#if defined(WOLFHSM_CFG_TEST_POSIX)
5756
if (ctx->transportType == WH_BENCH_TRANSPORT_POSIX_DMA) {
5857
/* if static memory was used with DMA then use XMALLOC */
@@ -66,6 +65,7 @@ int _benchSha256(whClientContext* client, whBenchOpContext* ctx, int id,
6665
out = XMALLOC(WC_SHA256_DIGEST_SIZE, heap, DYNAMIC_TYPE_TMP_BUFFER);
6766
if (out == NULL) {
6867
WH_BENCH_PRINTF("Failed to allocate memory for DMA\n");
68+
XFREE((uint8_t*)in, heap, DYNAMIC_TYPE_TMP_BUFFER);
6969
return WH_ERROR_NOSPACE;
7070
}
7171
}

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)