Skip to content

Commit d8f3885

Browse files
committed
Add DMA support for AES-ECB
1 parent 88b409a commit d8f3885

File tree

11 files changed

+628
-27
lines changed

11 files changed

+628
-27
lines changed

benchmark/bench_modules/wh_bench_mod_aes.c

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,137 @@ int wh_Bench_Mod_Aes256CTRDecryptDma(whClientContext* client,
380380

381381
#endif /* WOLFSSL_AES_COUNTER */
382382
#if defined(HAVE_AES_ECB)
383+
#if defined(WOLFHSM_CFG_DMA)
384+
static int _benchAesEcbDma(whClientContext* client, whBenchOpContext* ctx,
385+
int id, const uint8_t* key, size_t keyLen,
386+
int encrypt)
387+
{
388+
int ret = 0;
389+
int needEvict = 0;
390+
whKeyId keyId = WH_KEYID_ERASED;
391+
Aes aes[1];
392+
char keyLabel[] = "key label";
393+
const size_t inLen = WOLFHSM_CFG_BENCH_DMA_BUFFER_SIZE / 2;
394+
int i;
395+
const uint8_t* in = NULL;
396+
uint8_t* out = NULL;
397+
398+
#if defined(WOLFHSM_CFG_TEST_POSIX)
399+
/* Allocate buffers using XMALLOC with heap hints for DMA */
400+
if (ctx->transportType == WH_BENCH_TRANSPORT_POSIX_DMA) {
401+
void* heap =
402+
posixTransportShm_GetDmaHeap(client->comm->transport_context);
403+
in = XMALLOC(inLen, heap, DYNAMIC_TYPE_TMP_BUFFER);
404+
if (in == NULL) {
405+
WH_BENCH_PRINTF("Failed to allocate memory for DMA input\n");
406+
return WH_ERROR_NOSPACE;
407+
}
408+
409+
out = XMALLOC(inLen, heap, DYNAMIC_TYPE_TMP_BUFFER);
410+
if (out == NULL) {
411+
WH_BENCH_PRINTF("Failed to allocate memory for DMA output\n");
412+
XFREE((uint8_t*)in, heap, DYNAMIC_TYPE_TMP_BUFFER);
413+
return WH_ERROR_NOSPACE;
414+
}
415+
}
416+
else
417+
#endif /* WOLFHSM_CFG_TEST_POSIX */
418+
{
419+
in = WH_BENCH_DMA_BUFFER;
420+
out = (uint8_t*)in + inLen;
421+
}
422+
423+
#if defined(WOLFHSM_CFG_BENCH_INIT_DATA_BUFFERS)
424+
/* Initialize the input buffer with something non-zero */
425+
memset((uint8_t*)in, 0xAA, inLen);
426+
memset(out, 0xAA, inLen);
427+
#endif
428+
429+
/* Initialize the aes struct */
430+
ret = wc_AesInit(aes, NULL, WH_DEV_ID_DMA);
431+
if (ret != 0) {
432+
WH_BENCH_PRINTF("Failed to wc_AesInit %d\n", ret);
433+
goto exit;
434+
}
435+
436+
/* cache the key on the HSM */
437+
ret = wh_Client_KeyCache(client, 0, (uint8_t*)keyLabel, sizeof(keyLabel),
438+
(uint8_t*)key, keyLen, &keyId);
439+
if (ret != 0) {
440+
WH_BENCH_PRINTF("Failed to wh_Client_KeyCache %d\n", ret);
441+
goto exit;
442+
}
443+
444+
needEvict = 1;
445+
446+
/* set the keyId on the struct */
447+
ret = wh_Client_AesSetKeyId(aes, keyId);
448+
if (ret != 0) {
449+
WH_BENCH_PRINTF("Failed to wh_Client_SetKeyIdAes %d\n", ret);
450+
goto exit;
451+
}
452+
453+
ret = wh_Bench_SetDataSize(ctx, id, inLen);
454+
if (ret != 0) {
455+
WH_BENCH_PRINTF("Failed to wh_Bench_SetDataSize %d\n", ret);
456+
goto exit;
457+
}
458+
459+
/* Perform the benchmark */
460+
for (i = 0; i < WOLFHSM_CFG_BENCH_CRYPT_ITERS; i++) {
461+
int benchStartRet;
462+
int benchStopRet;
463+
464+
if (encrypt) {
465+
benchStartRet = wh_Bench_StartOp(ctx, id);
466+
ret = wc_AesEcbEncrypt(aes, out, in, inLen);
467+
benchStopRet = wh_Bench_StopOp(ctx, id);
468+
}
469+
else {
470+
benchStartRet = wh_Bench_StartOp(ctx, id);
471+
ret = wc_AesEcbDecrypt(aes, out, in, inLen);
472+
benchStopRet = wh_Bench_StopOp(ctx, id);
473+
}
474+
475+
if (benchStartRet != 0) {
476+
WH_BENCH_PRINTF("Failed to wh_Bench_StartOp %d\n", benchStartRet);
477+
ret = benchStartRet;
478+
goto exit;
479+
}
480+
if (ret != 0) {
481+
WH_BENCH_PRINTF("Failed to wh_Client_AesCbcDma %d\n", ret);
482+
goto exit;
483+
}
484+
if (benchStopRet != 0) {
485+
WH_BENCH_PRINTF("Failed to wh_Bench_StopOp %d\n", benchStopRet);
486+
ret = benchStopRet;
487+
goto exit;
488+
}
489+
}
490+
491+
exit:
492+
if (needEvict) {
493+
(void)wh_Client_KeyEvict(client, keyId);
494+
}
495+
wc_AesFree(aes);
496+
497+
#if defined(WOLFHSM_CFG_TEST_POSIX)
498+
if (ctx->transportType == WH_BENCH_TRANSPORT_POSIX_DMA) {
499+
void* heap =
500+
posixTransportShm_GetDmaHeap(client->comm->transport_context);
501+
if (in != NULL) {
502+
XFREE((uint8_t*)in, heap, DYNAMIC_TYPE_TMP_BUFFER);
503+
}
504+
if (out != NULL) {
505+
XFREE(out, heap, DYNAMIC_TYPE_TMP_BUFFER);
506+
}
507+
}
508+
#endif /* WOLFHSM_CFG_TEST_POSIX */
509+
510+
return ret;
511+
}
512+
#endif /* WOLFHSM_CFG_DMA */
513+
383514
static int _benchAesEcb(whClientContext* client, whBenchOpContext* ctx, int id,
384515
const uint8_t* key, size_t keyLen, int encrypt)
385516
{
@@ -510,6 +641,74 @@ int wh_Bench_Mod_Aes256ECBDecrypt(whClientContext* client,
510641
return _benchAesEcb(client, ctx, id, (uint8_t*)key256, sizeof(key256),
511642
DECRYPT);
512643
}
644+
645+
int wh_Bench_Mod_Aes128ECBEncryptDma(whClientContext* client,
646+
whBenchOpContext* ctx, int id,
647+
void* params)
648+
{
649+
#if defined(WOLFHSM_CFG_DMA)
650+
(void)params;
651+
return _benchAesEcbDma(client, ctx, id, (uint8_t*)key128, sizeof(key128),
652+
ENCRYPT);
653+
#else
654+
(void)client;
655+
(void)ctx;
656+
(void)id;
657+
(void)params;
658+
return WH_ERROR_NOTIMPL;
659+
#endif
660+
}
661+
662+
int wh_Bench_Mod_Aes128ECBDecryptDma(whClientContext* client,
663+
whBenchOpContext* ctx, int id,
664+
void* params)
665+
{
666+
#if defined(WOLFHSM_CFG_DMA)
667+
(void)params;
668+
return _benchAesEcbDma(client, ctx, id, (uint8_t*)key128, sizeof(key128),
669+
DECRYPT);
670+
#else
671+
(void)client;
672+
(void)ctx;
673+
(void)id;
674+
(void)params;
675+
return WH_ERROR_NOTIMPL;
676+
#endif
677+
}
678+
679+
int wh_Bench_Mod_Aes256ECBEncryptDma(whClientContext* client,
680+
whBenchOpContext* ctx, int id,
681+
void* params)
682+
{
683+
#if defined(WOLFHSM_CFG_DMA)
684+
(void)params;
685+
return _benchAesEcbDma(client, ctx, id, (uint8_t*)key256, sizeof(key256),
686+
ENCRYPT);
687+
#else
688+
(void)client;
689+
(void)ctx;
690+
(void)id;
691+
(void)params;
692+
return WH_ERROR_NOTIMPL;
693+
#endif
694+
}
695+
696+
int wh_Bench_Mod_Aes256ECBDecryptDma(whClientContext* client,
697+
whBenchOpContext* ctx, int id,
698+
void* params)
699+
{
700+
#if defined(WOLFHSM_CFG_DMA)
701+
(void)params;
702+
return _benchAesEcbDma(client, ctx, id, (uint8_t*)key256, sizeof(key256),
703+
DECRYPT);
704+
#else
705+
(void)client;
706+
(void)ctx;
707+
(void)id;
708+
(void)params;
709+
return WH_ERROR_NOTIMPL;
710+
#endif
711+
}
513712
#endif /* HAVE_AES_ECB */
514713

515714
#if defined(HAVE_AES_CBC)

benchmark/bench_modules/wh_bench_mod_all.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ int wh_Bench_Mod_Aes128ECBEncrypt(whClientContext* client,
4848
int wh_Bench_Mod_Aes128ECBDecrypt(whClientContext* client,
4949
whBenchOpContext* ctx, int id, void* params);
5050

51+
int wh_Bench_Mod_Aes128ECBEncryptDma(whClientContext* client,
52+
whBenchOpContext* ctx, int id,
53+
void* params);
54+
55+
int wh_Bench_Mod_Aes128ECBDecryptDma(whClientContext* client,
56+
whBenchOpContext* ctx, int id,
57+
void* params);
58+
5159
int wh_Bench_Mod_Aes128CBCEncrypt(whClientContext* client,
5260
whBenchOpContext* ctx, int id, void* params);
5361

@@ -96,6 +104,14 @@ int wh_Bench_Mod_Aes256ECBEncrypt(whClientContext* client,
96104
int wh_Bench_Mod_Aes256ECBDecrypt(whClientContext* client,
97105
whBenchOpContext* ctx, int id, void* params);
98106

107+
int wh_Bench_Mod_Aes256ECBEncryptDma(whClientContext* client,
108+
whBenchOpContext* ctx, int id,
109+
void* params);
110+
111+
int wh_Bench_Mod_Aes256ECBDecryptDma(whClientContext* client,
112+
whBenchOpContext* ctx, int id,
113+
void* params);
114+
99115
int wh_Bench_Mod_Aes256CBCEncrypt(whClientContext* client,
100116
whBenchOpContext* ctx, int id, void* params);
101117

benchmark/wh_bench.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,12 @@ typedef enum BenchModuleIdx {
9797
#if defined(HAVE_AES_ECB)
9898
BENCH_MODULE_IDX_AES_128_ECB_ENCRYPT,
9999
BENCH_MODULE_IDX_AES_128_ECB_DECRYPT,
100+
BENCH_MODULE_IDX_AES_128_ECB_ENCRYPT_DMA,
101+
BENCH_MODULE_IDX_AES_128_ECB_DECRYPT_DMA,
100102
BENCH_MODULE_IDX_AES_256_ECB_ENCRYPT,
101103
BENCH_MODULE_IDX_AES_256_ECB_DECRYPT,
104+
BENCH_MODULE_IDX_AES_256_ECB_ENCRYPT_DMA,
105+
BENCH_MODULE_IDX_AES_256_ECB_DECRYPT_DMA,
102106
#endif /* HAVE_AES_ECB */
103107
#if defined(HAVE_AES_CBC)
104108
BENCH_MODULE_IDX_AES_128_CBC_ENCRYPT,
@@ -283,8 +287,12 @@ static BenchModule g_benchModules[] = {
283287
#if defined(HAVE_AES_ECB)
284288
[BENCH_MODULE_IDX_AES_128_ECB_ENCRYPT] = {"AES-128-ECB-Encrypt", wh_Bench_Mod_Aes128ECBEncrypt, BENCH_THROUGHPUT_XBPS, 0, NULL},
285289
[BENCH_MODULE_IDX_AES_128_ECB_DECRYPT] = {"AES-128-ECB-Decrypt", wh_Bench_Mod_Aes128ECBDecrypt, BENCH_THROUGHPUT_XBPS, 0, NULL},
290+
[BENCH_MODULE_IDX_AES_128_ECB_ENCRYPT_DMA] = {"AES-128-ECB-Encrypt-DMA", wh_Bench_Mod_Aes128ECBEncryptDma, BENCH_THROUGHPUT_XBPS, 0, NULL},
291+
[BENCH_MODULE_IDX_AES_128_ECB_DECRYPT_DMA] = {"AES-128-ECB-Decrypt-DMA", wh_Bench_Mod_Aes128ECBDecryptDma, BENCH_THROUGHPUT_XBPS, 0, NULL},
286292
[BENCH_MODULE_IDX_AES_256_ECB_ENCRYPT] = {"AES-256-ECB-Encrypt", wh_Bench_Mod_Aes256ECBEncrypt, BENCH_THROUGHPUT_XBPS, 0, NULL},
287293
[BENCH_MODULE_IDX_AES_256_ECB_DECRYPT] = {"AES-256-ECB-Decrypt", wh_Bench_Mod_Aes256ECBDecrypt, BENCH_THROUGHPUT_XBPS, 0, NULL},
294+
[BENCH_MODULE_IDX_AES_256_ECB_ENCRYPT_DMA] = {"AES-256-ECB-Encrypt-DMA", wh_Bench_Mod_Aes256ECBEncryptDma, BENCH_THROUGHPUT_XBPS, 0, NULL},
295+
[BENCH_MODULE_IDX_AES_256_ECB_DECRYPT_DMA] = {"AES-256-ECB-Decrypt-DMA", wh_Bench_Mod_Aes256ECBDecryptDma, BENCH_THROUGHPUT_XBPS, 0, NULL},
288296
#endif /* HAVE_AES_ECB */
289297
#if defined(HAVE_AES_CBC)
290298
[BENCH_MODULE_IDX_AES_128_CBC_ENCRYPT] = {"AES-128-CBC-Encrypt", wh_Bench_Mod_Aes128CBCEncrypt, BENCH_THROUGHPUT_XBPS, 0, NULL},

benchmark/wh_bench_ops.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include <stdint.h>
2727

2828
/* Maximum number of operations that can be registered */
29-
#define MAX_BENCH_OPS 99
29+
#define MAX_BENCH_OPS 103
3030
/* Maximum length of operation name */
3131
#define MAX_OP_NAME 64
3232

0 commit comments

Comments
 (0)