Skip to content

Commit 65ddce3

Browse files
JacobBarthelmehjackctj117
authored andcommitted
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 05a80f6 commit 65ddce3

File tree

13 files changed

+460
-151
lines changed

13 files changed

+460
-151
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
}

examples/demo/client/wh_demo_client_crypto.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,7 +1548,7 @@ int wh_DemoClient_CryptoCmacKdfExport(whClientContext* clientContext)
15481548
(word32)sizeof(demoCmacKdfFixedInfo), derived, (word32)sizeof(derived),
15491549
NULL, WH_DEV_ID);
15501550
if (ret != 0) {
1551-
printf("Failed to wc_KDA_KDF_twostep_cmac %d\n", ret);
1551+
WOLFHSM_CFG_PRINTF("Failed to wc_KDA_KDF_twostep_cmac %d\n", ret);
15521552
}
15531553

15541554
/* The key has now been derived and is stored in the 'derived' array */
@@ -1576,7 +1576,7 @@ int wh_DemoClient_CryptoCmacKdfCache(whClientContext* clientContext)
15761576
(const uint8_t*)label, (uint32_t)strlen(label),
15771577
WH_DEMO_CMAC_KDF_OUT_SZ);
15781578
if (ret != WH_ERROR_OK) {
1579-
printf("Failed to wh_Client_CmacKdfMakeCacheKey %d\n", ret);
1579+
WOLFHSM_CFG_PRINTF("Failed to wh_Client_CmacKdfMakeCacheKey %d\n", ret);
15801580
return ret;
15811581
}
15821582

@@ -1586,7 +1586,7 @@ int wh_DemoClient_CryptoCmacKdfCache(whClientContext* clientContext)
15861586
/* Example: evict the key from cache once we are done with it */
15871587
ret = wh_Client_KeyEvict(clientContext, keyId);
15881588
if (ret != 0) {
1589-
printf("Failed to wh_Client_KeyEvict %d\n", evictRet);
1589+
WOLFHSM_CFG_PRINTF("Failed to wh_Client_KeyEvict %d\n", evictRet);
15901590
}
15911591

15921592
return ret;
@@ -1613,7 +1613,7 @@ int wh_DemoClient_CryptoCmacKdfCacheInputs(whClientContext* clientContext)
16131613
demoCmacKdfSalt, (uint32_t)sizeof(demoCmacKdfSalt),
16141614
&saltKeyId);
16151615
if (ret != WH_ERROR_OK) {
1616-
printf("Failed to cache CMAC KDF salt %d\n", ret);
1616+
WOLFHSM_CFG_PRINTF("Failed to cache CMAC KDF salt %d\n", ret);
16171617
return ret;
16181618
}
16191619

@@ -1623,7 +1623,7 @@ int wh_DemoClient_CryptoCmacKdfCacheInputs(whClientContext* clientContext)
16231623
demoCmacKdfZ, (uint32_t)sizeof(demoCmacKdfZ),
16241624
&zKeyId);
16251625
if (ret != WH_ERROR_OK) {
1626-
printf("Failed to cache CMAC KDF Z input %d\n", ret);
1626+
WOLFHSM_CFG_PRINTF("Failed to cache CMAC KDF Z input %d\n", ret);
16271627
/* Optionally evict the salt key if not needed anymore */
16281628
(void)wh_Client_KeyEvict(clientContext, saltKeyId);
16291629
return ret;
@@ -1643,7 +1643,7 @@ int wh_DemoClient_CryptoCmacKdfCacheInputs(whClientContext* clientContext)
16431643
WH_DEMO_CMAC_KDF_OUT_SZ);
16441644

16451645
if (ret != WH_ERROR_OK) {
1646-
printf("Failed to CMAC KDF with cached inputs %d\n", ret);
1646+
WOLFHSM_CFG_PRINTF("Failed to CMAC KDF with cached inputs %d\n", ret);
16471647
}
16481648

16491649

examples/posix/wh_posix_client/wh_posix_client.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static int wh_ClientTask(void* cf, const char* type, int test)
7272
if (ret == 0) {
7373
ret = wh_Client_CommInit(client, NULL, NULL);
7474
if (ret != 0) {
75-
printf("Failed to initialize client communication\n");
75+
WOLFHSM_CFG_PRINTF("Failed to initialize client communication\n");
7676
return -1;
7777
}
7878
}
@@ -93,7 +93,7 @@ static int wh_ClientTask(void* cf, const char* type, int test)
9393

9494
WOLFHSM_CFG_PRINTF("Client connecting to server...\n");
9595
if (ret == 0 && test) {
96-
printf("Running client demos...\n");
96+
WOLFHSM_CFG_PRINTF("Running client demos...\n");
9797
return wh_DemoClient_All(client);
9898
}
9999

examples/posix/wh_posix_server/wh_posix_server.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ int main(int argc, char** argv)
420420
(void)clientId;
421421
rc = wh_ServerTask(s_conf, keyFilePath, keyId, clientId);
422422
if (rc != WH_ERROR_OK) {
423-
printf("Server task failed: %d\n", rc);
423+
WOLFHSM_CFG_PRINTF("Server task failed: %d\n", rc);
424424
return rc;
425425
}
426426
#endif

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)