Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions benchmark/bench_modules/wh_bench_mod_cmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
#if !defined(WOLFHSM_CFG_NO_CRYPTO) && defined(WOLFHSM_CFG_BENCH_ENABLE)
#include "wolfssl/wolfcrypt/cmac.h"

#if defined(WOLFHSM_CFG_DMA) && defined(WOLFHSM_CFG_TEST_POSIX)
#include "port/posix/posix_transport_shm.h"
#endif /* WOLFHSM_CFG_DMA && WOLFHSM_CFG_TEST_POSIX */

#if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)

static const uint8_t key128[] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae,
Expand All @@ -44,10 +48,11 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
whKeyId keyId = WH_KEYID_ERASED;
Cmac cmac[1];
char keyLabel[] = "baby's first key";
byte tag[16];
byte tag[WC_CMAC_TAG_MAX_SZ];
int i;
uint8_t* in = NULL;
size_t inLen;
uint8_t* out = NULL;

/* cache the key on the HSM */
ret = wh_Client_KeyCache(client, 0, (uint8_t*)keyLabel, sizeof(keyLabel),
Expand All @@ -57,10 +62,32 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
return ret;
}

out = tag; /* default to using tag buffer on the stack */
#if defined(WOLFHSM_CFG_DMA)
if (devId == WH_DEV_ID_DMA) {
in = WH_BENCH_DMA_BUFFER;
inLen = WOLFHSM_CFG_BENCH_DMA_BUFFER_SIZE;
#if defined(WOLFHSM_CFG_TEST_POSIX)
if (ctx->transportType == WH_BENCH_TRANSPORT_POSIX_DMA) {
Comment thread
bigbrett marked this conversation as resolved.
/* if static memory was used with DMA then use XMALLOC */
void* heap =
posixTransportShm_GetDmaHeap(client->comm->transport_context);
in = (uint8_t*)XMALLOC(inLen, heap, DYNAMIC_TYPE_TMP_BUFFER);
if (in == NULL) {
WH_BENCH_PRINTF("Failed to allocate memory for DMA\n");
return WH_ERROR_NOSPACE;
}
out = (uint8_t*)XMALLOC(WC_CMAC_TAG_MAX_SZ, heap,
DYNAMIC_TYPE_TMP_BUFFER);
if (out == NULL) {
WH_BENCH_PRINTF("Failed to allocate memory for DMA\n");
XFREE(in, heap, DYNAMIC_TYPE_TMP_BUFFER);
return WH_ERROR_NOSPACE;
Comment thread
bigbrett marked this conversation as resolved.
}
}
else {
in = WH_BENCH_DMA_BUFFER;
}
#endif /* WOLFHSM_CFG_TEST_POSIX */
}
else
#endif
Expand Down Expand Up @@ -101,7 +128,7 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
benchStartRet = wh_Bench_StartOp(ctx, id);
/* Oneshot CMAC through wolfCrypt API will always be most performant
* implementation */
ret = wc_AesCmacGenerate_ex(cmac, tag, &outLen, in, inLen, key, keyLen,
ret = wc_AesCmacGenerate_ex(cmac, out, &outLen, in, inLen, key, keyLen,
NULL, devId);
benchStopRet = wh_Bench_StopOp(ctx, id);

Expand Down Expand Up @@ -131,6 +158,18 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
ret = evictRet;
}
}
#if defined(WOLFHSM_CFG_DMA)
#if defined(WOLFHSM_CFG_TEST_POSIX)
if (devId == WH_DEV_ID_DMA &&
ctx->transportType == WH_BENCH_TRANSPORT_POSIX_DMA) {
/* if static memory was used with DMA then use XFREE */
void* heap =
posixTransportShm_GetDmaHeap(client->comm->transport_context);
XFREE(in, heap, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(out, heap, DYNAMIC_TYPE_TMP_BUFFER);
}
#endif /* WOLFHSM_CFG_TEST_POSIX */
#endif
(void)wc_CmacFree(cmac);
return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion benchmark/bench_modules/wh_bench_mod_sha2.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ int _benchSha256(whClientContext* client, whBenchOpContext* ctx, int id,
#if defined(WOLFHSM_CFG_DMA)
if (devId == WH_DEV_ID_DMA) {
inLen = WOLFHSM_CFG_BENCH_DMA_BUFFER_SIZE;

#if defined(WOLFHSM_CFG_TEST_POSIX)
if (ctx->transportType == WH_BENCH_TRANSPORT_POSIX_DMA) {
/* if static memory was used with DMA then use XMALLOC */
Expand All @@ -66,6 +65,7 @@ int _benchSha256(whClientContext* client, whBenchOpContext* ctx, int id,
out = XMALLOC(WC_SHA256_DIGEST_SIZE, heap, DYNAMIC_TYPE_TMP_BUFFER);
if (out == NULL) {
WH_BENCH_PRINTF("Failed to allocate memory for DMA\n");
XFREE((uint8_t*)in, heap, DYNAMIC_TYPE_TMP_BUFFER);
return WH_ERROR_NOSPACE;
}
}
Expand Down
18 changes: 15 additions & 3 deletions src/wh_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,9 @@ int wh_Client_KeyCacheDmaRequest(whClientContext* c, uint32_t flags,
const void* keyAddr, uint16_t keySz,
uint16_t keyId)
{
int ret;
whMessageKeystore_CacheDmaRequest* req = NULL;
uintptr_t keyAddrPtr = 0;

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

/* Set up DMA buffer info */
req->key.addr = (uint64_t)((uintptr_t)keyAddr);
req->key.sz = keySz;
ret = wh_Client_DmaProcessClientAddress(
c, (uintptr_t)keyAddr, (void**)&keyAddrPtr, keySz,
WH_DMA_OPER_CLIENT_READ_PRE, (whDmaFlags){0});
req->key.addr = keyAddrPtr;

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

return wh_Client_SendRequest(c, WH_MESSAGE_GROUP_KEY, WH_KEY_CACHE_DMA,
sizeof(*req), (uint8_t*)req);
if (ret == WH_ERROR_OK) {
ret = wh_Client_SendRequest(c, WH_MESSAGE_GROUP_KEY, WH_KEY_CACHE_DMA,
sizeof(*req), (uint8_t*)req);
}

(void)wh_Client_DmaProcessClientAddress(
c, (uintptr_t)keyAddr, (void**)&keyAddrPtr, keySz,
WH_DMA_OPER_CLIENT_READ_POST, (whDmaFlags){0});
return ret;
}

int wh_Client_KeyCacheDmaResponse(whClientContext* c, uint16_t* keyId)
Expand Down
Loading