Skip to content
32 changes: 24 additions & 8 deletions benchmark/bench_modules/wh_bench_mod_aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ static int _benchAesCtrDma(whClientContext* client, whBenchOpContext* ctx,
const uint8_t* in = NULL;
uint8_t* out = NULL;

(void)wh_Client_SetDmaMode(client, 1);

#if defined(WOLFHSM_CFG_TEST_POSIX)
/* Allocate buffers using XMALLOC with heap hints for DMA */
if (ctx->transportType == WH_BENCH_TRANSPORT_POSIX_DMA) {
Expand Down Expand Up @@ -90,7 +92,7 @@ static int _benchAesCtrDma(whClientContext* client, whBenchOpContext* ctx,
#endif

/* Initialize the aes struct */
ret = wc_AesInit(aes, NULL, WH_DEV_ID_DMA);
ret = wc_AesInit(aes, NULL, WH_CLIENT_DEVID(client));
if (ret != 0) {
WH_BENCH_PRINTF("Failed to wc_AesInit %d\n", ret);
goto exit;
Expand Down Expand Up @@ -186,14 +188,16 @@ static int _benchAesCtr(whClientContext* client, whBenchOpContext* ctx, int id,
WC_AES_BLOCK_SIZE;
int i;

(void)wh_Client_SetDmaMode(client, 0);

#if defined(WOLFHSM_CFG_BENCH_INIT_DATA_BUFFERS)
/* Initialize the input buffer with something non-zero */
memset(WH_BENCH_DATA_IN_BUFFER, 0xAA, inLen);
memset(WH_BENCH_DATA_OUT_BUFFER, 0xAA, inLen);
#endif

/* Initialize the aes struct */
ret = wc_AesInit(aes, NULL, WH_DEV_ID);
ret = wc_AesInit(aes, NULL, WH_CLIENT_DEVID(client));
if (ret != 0) {
WH_BENCH_PRINTF("Failed to wc_AesInit %d\n", ret);
goto exit;
Expand Down Expand Up @@ -382,6 +386,8 @@ static int _benchAesEcbDma(whClientContext* client, whBenchOpContext* ctx,
const uint8_t* in = NULL;
uint8_t* out = NULL;

(void)wh_Client_SetDmaMode(client, 1);

#if defined(WOLFHSM_CFG_TEST_POSIX)
/* Allocate buffers using XMALLOC with heap hints for DMA */
if (ctx->transportType == WH_BENCH_TRANSPORT_POSIX_DMA) {
Expand Down Expand Up @@ -414,7 +420,7 @@ static int _benchAesEcbDma(whClientContext* client, whBenchOpContext* ctx,
#endif

/* Initialize the aes struct */
ret = wc_AesInit(aes, NULL, WH_DEV_ID_DMA);
ret = wc_AesInit(aes, NULL, WH_CLIENT_DEVID(client));
if (ret != 0) {
WH_BENCH_PRINTF("Failed to wc_AesInit %d\n", ret);
goto exit;
Expand Down Expand Up @@ -513,14 +519,16 @@ static int _benchAesEcb(whClientContext* client, whBenchOpContext* ctx, int id,
WC_AES_BLOCK_SIZE;
int i;

(void)wh_Client_SetDmaMode(client, 0);

#if defined(WOLFHSM_CFG_BENCH_INIT_DATA_BUFFERS)
/* Initialize the input buffer with something non-zero */
memset(WH_BENCH_DATA_IN_BUFFER, 0xAA, inLen);
memset(WH_BENCH_DATA_OUT_BUFFER, 0xAA, inLen);
#endif

/* Initialize the aes struct */
ret = wc_AesInit(aes, NULL, WH_DEV_ID);
ret = wc_AesInit(aes, NULL, WH_CLIENT_DEVID(client));
if (ret != 0) {
WH_BENCH_PRINTF("Failed to wc_AesInit %d\n", ret);
goto exit;
Expand Down Expand Up @@ -717,6 +725,8 @@ static int _benchAesCbcDma(whClientContext* client, whBenchOpContext* ctx,
const uint8_t* in = NULL;
uint8_t* out = NULL;

(void)wh_Client_SetDmaMode(client, 1);

#if defined(WOLFHSM_CFG_TEST_POSIX)
/* Allocate buffers using XMALLOC with heap hints for DMA */
if (ctx->transportType == WH_BENCH_TRANSPORT_POSIX_DMA) {
Expand Down Expand Up @@ -749,7 +759,7 @@ static int _benchAesCbcDma(whClientContext* client, whBenchOpContext* ctx,
#endif

/* Initialize the aes struct */
ret = wc_AesInit(aes, NULL, WH_DEV_ID_DMA);
ret = wc_AesInit(aes, NULL, WH_CLIENT_DEVID(client));
if (ret != 0) {
WH_BENCH_PRINTF("Failed to wc_AesInit %d\n", ret);
goto exit;
Expand Down Expand Up @@ -857,14 +867,16 @@ static int _benchAesCbc(whClientContext* client, whBenchOpContext* ctx, int id,
WC_AES_BLOCK_SIZE;
int i;

(void)wh_Client_SetDmaMode(client, 0);

#if defined(WOLFHSM_CFG_BENCH_INIT_DATA_BUFFERS)
/* Initialize the input buffer with something non-zero */
memset(WH_BENCH_DATA_IN_BUFFER, 0xAA, inLen);
memset(WH_BENCH_DATA_OUT_BUFFER, 0xAA, inLen);
#endif

/* Initialize the aes struct */
ret = wc_AesInit(aes, NULL, WH_DEV_ID);
ret = wc_AesInit(aes, NULL, WH_CLIENT_DEVID(client));
if (ret != 0) {
WH_BENCH_PRINTF("Failed to wc_AesInit %d\n", ret);
goto exit;
Expand Down Expand Up @@ -1073,6 +1085,8 @@ static int _benchAesGcmDma(whClientContext* client, whBenchOpContext* ctx,
const uint8_t* in = NULL;
uint8_t* out = NULL;

(void)wh_Client_SetDmaMode(client, 1);

#if defined(WOLFHSM_CFG_TEST_POSIX)
/* Allocate buffers using XMALLOC with heap hints for DMA */
if (ctx->transportType == WH_BENCH_TRANSPORT_POSIX_DMA) {
Expand Down Expand Up @@ -1105,7 +1119,7 @@ static int _benchAesGcmDma(whClientContext* client, whBenchOpContext* ctx,
#endif

/* initialize the aes struct */
ret = wc_AesInit(aes, NULL, WH_DEV_ID_DMA);
ret = wc_AesInit(aes, NULL, WH_CLIENT_DEVID(client));
if (ret != 0) {
WH_BENCH_PRINTF("Failed to wc_AesInit %d\n", ret);
goto exit;
Expand Down Expand Up @@ -1230,14 +1244,16 @@ static int _benchAesGcm(whClientContext* client, whBenchOpContext* ctx, int id,
WC_AES_BLOCK_SIZE;
int i;

(void)wh_Client_SetDmaMode(client, 0);

#if defined(WOLFHSM_CFG_BENCH_INIT_DATA_BUFFERS)
/* Initialize the input buffer with something non-zero */
memset(WH_BENCH_DATA_IN_BUFFER, 0xAA, inLen);
memset(WH_BENCH_DATA_OUT_BUFFER, 0xAA, inLen);
#endif

/* initialize the aes struct */
ret = wc_AesInit(aes, NULL, WH_DEV_ID);
ret = wc_AesInit(aes, NULL, WH_CLIENT_DEVID(client));
if (ret != 0) {
WH_BENCH_PRINTF("Failed to wc_AesInit %d\n", ret);
goto exit;
Expand Down
24 changes: 12 additions & 12 deletions benchmark/bench_modules/wh_bench_mod_cmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static const uint8_t key256[] = {


int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
const uint8_t* key, size_t keyLen, int devId)
const uint8_t* key, size_t keyLen, int useDma)
{
int ret = 0;
word32 outLen;
Expand All @@ -54,6 +54,8 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
size_t inLen;
uint8_t* out = NULL;

(void)wh_Client_SetDmaMode(client, useDma);

/* cache the key on the HSM */
ret = wh_Client_KeyCache(client, WH_NVM_FLAGS_USAGE_ANY, (uint8_t*)keyLabel,
sizeof(keyLabel), (uint8_t*)key, keyLen, &keyId);
Expand All @@ -64,7 +66,7 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,

out = tag; /* default to using tag buffer on the stack */
#if defined(WOLFHSM_CFG_DMA)
if (devId == WH_DEV_ID_DMA) {
if (useDma) {
inLen = WOLFHSM_CFG_BENCH_DMA_BUFFER_SIZE;
#if defined(WOLFHSM_CFG_TEST_POSIX)
if (ctx->transportType == WH_BENCH_TRANSPORT_POSIX_DMA) {
Expand Down Expand Up @@ -111,7 +113,8 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
int benchStopRet;

/* initialize the cmac struct */
ret = wc_InitCmac_ex(cmac, NULL, 0, WC_CMAC_AES, NULL, NULL, devId);
ret = wc_InitCmac_ex(cmac, NULL, 0, WC_CMAC_AES, NULL, NULL,
WH_CLIENT_DEVID(client));
if (ret != 0) {
WH_BENCH_PRINTF("Failed to wc_InitCmac_ex %d\n", ret);
goto exit;
Expand All @@ -129,7 +132,7 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
/* Oneshot CMAC through wolfCrypt API will always be most performant
* implementation */
ret = wc_AesCmacGenerate_ex(cmac, out, &outLen, in, inLen, key, keyLen,
NULL, devId);
NULL, WH_CLIENT_DEVID(client));
benchStopRet = wh_Bench_StopOp(ctx, id);

if (benchStartRet != 0) {
Expand Down Expand Up @@ -160,8 +163,7 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
}
#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 (useDma && 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);
Expand All @@ -178,16 +180,15 @@ int wh_Bench_Mod_CmacAes128(whClientContext* client, whBenchOpContext* ctx,
int id, void* params)
{
(void)params;
return _benchCmacAes(client, ctx, id, key128, sizeof(key128), WH_DEV_ID);
return _benchCmacAes(client, ctx, id, key128, sizeof(key128), 0);
}

int wh_Bench_Mod_CmacAes128Dma(whClientContext* client, whBenchOpContext* ctx,
int id, void* params)
{
#if defined(WOLFHSM_CFG_DMA)
(void)params;
return _benchCmacAes(client, ctx, id, key128, sizeof(key128),
WH_DEV_ID_DMA);
return _benchCmacAes(client, ctx, id, key128, sizeof(key128), 1);
#else
(void)client;
(void)ctx;
Expand All @@ -201,16 +202,15 @@ int wh_Bench_Mod_CmacAes256(whClientContext* client, whBenchOpContext* ctx,
int id, void* params)
{
(void)params;
return _benchCmacAes(client, ctx, id, key256, sizeof(key256), WH_DEV_ID);
return _benchCmacAes(client, ctx, id, key256, sizeof(key256), 0);
}

int wh_Bench_Mod_CmacAes256Dma(whClientContext* client, whBenchOpContext* ctx,
int id, void* params)
{
#if defined(WOLFHSM_CFG_DMA)
(void)params;
return _benchCmacAes(client, ctx, id, key256, sizeof(key256),
WH_DEV_ID_DMA);
return _benchCmacAes(client, ctx, id, key256, sizeof(key256), 1);
#else
(void)client;
(void)ctx;
Expand Down
6 changes: 3 additions & 3 deletions benchmark/bench_modules/wh_bench_mod_cmac_kdf.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#define WH_BENCH_CMAC_KDF_OUT_SIZE 40

static int _benchCmacKdf(whClientContext* client, whBenchOpContext* ctx, int id,
int devId)
int useDma)
{
/* Derivation inputs mirror the unit test vectors to provide realistic
* message sizes while keeping the benchmark deterministic. */
Expand All @@ -55,7 +55,7 @@ static int _benchCmacKdf(whClientContext* client, whBenchOpContext* ctx, int id,
whKeyId keyId;
int i;

(void)devId;
(void)wh_Client_SetDmaMode(client, useDma);

for (i = 0; i < WOLFHSM_CFG_BENCH_KG_ITERS && ret == 0; i++) {
int benchStartRet;
Expand Down Expand Up @@ -103,7 +103,7 @@ int wh_Bench_Mod_CmacKdf(whClientContext* client, whBenchOpContext* ctx, int id,
void* params)
{
(void)params;
return _benchCmacKdf(client, ctx, id, WH_DEV_ID);
return _benchCmacKdf(client, ctx, id, 0);
}

#endif /* HAVE_CMAC_KDF && WOLFSSL_CMAC */
Expand Down
13 changes: 8 additions & 5 deletions benchmark/bench_modules/wh_bench_mod_curve25519.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ uint8_t key2_der[] = {
int wh_Bench_Mod_Curve25519KeyGen(whClientContext* client,
whBenchOpContext* ctx, int id, void* params)
{
(void)client;
(void)params;

int ret = 0;
Expand All @@ -62,8 +61,10 @@ int wh_Bench_Mod_Curve25519KeyGen(whClientContext* client,
int initialized_rng = 0;
int initialized_key = 0;

(void)wh_Client_SetDmaMode(client, 0);

/* Initialize the RNG for key generation */
ret = wc_InitRng_ex(rng, NULL, WH_DEV_ID);
ret = wc_InitRng_ex(rng, NULL, WH_CLIENT_DEVID(client));
if (ret != 0) {
WH_BENCH_PRINTF("Failed to wc_InitRng_ex %d\n", ret);
return ret;
Expand All @@ -76,7 +77,7 @@ int wh_Bench_Mod_Curve25519KeyGen(whClientContext* client,
int benchStopRet;

/* Initialize the Curve25519 key before each iteration */
ret = wc_curve25519_init_ex(key, NULL, WH_DEV_ID);
ret = wc_curve25519_init_ex(key, NULL, WH_CLIENT_DEVID(client));
if (ret != 0) {
WH_BENCH_PRINTF("Failed to wc_curve25519_init_ex %d\n", ret);
break;
Expand Down Expand Up @@ -140,6 +141,8 @@ int wh_Bench_Mod_Curve25519SharedSecret(whClientContext* client,
whKeyId keyIdBob = WH_KEYID_ERASED;
char keyLabel[] = "bench-key";

(void)wh_Client_SetDmaMode(client, 0);

/* Cache Alice's key in the HSM */
ret = wh_Client_KeyCache(client, WH_NVM_FLAGS_USAGE_ANY, (uint8_t*)keyLabel,
strlen(keyLabel), key1_der, sizeof(key1_der),
Expand All @@ -160,7 +163,7 @@ int wh_Bench_Mod_Curve25519SharedSecret(whClientContext* client,
}

/* Initialize Alice's key structure */
ret = wc_curve25519_init_ex(keyAlice, NULL, WH_DEV_ID);
ret = wc_curve25519_init_ex(keyAlice, NULL, WH_CLIENT_DEVID(client));
if (ret != 0) {
WH_BENCH_PRINTF("Failed to initialize Alice's key %d\n", ret);
wh_Client_KeyEvict(client, keyIdAlice);
Expand All @@ -177,7 +180,7 @@ int wh_Bench_Mod_Curve25519SharedSecret(whClientContext* client,
}

/* Initialize Bob's key structure */
ret = wc_curve25519_init_ex(keyBob, NULL, WH_DEV_ID);
ret = wc_curve25519_init_ex(keyBob, NULL, WH_CLIENT_DEVID(client));
if (ret != 0) {
WH_BENCH_PRINTF("Failed to initialize Bob's key %d\n", ret);
goto exit;
Expand Down
Loading