Skip to content

Commit 5cfda7c

Browse files
committed
Add DMA support for AES-ECB
Also minor fixes to the non-DMA AES-ECB code
1 parent 2ae3ea2 commit 5cfda7c

11 files changed

Lines changed: 658 additions & 41 deletions

benchmark/bench_modules/wh_bench_mod_aes.c

Lines changed: 201 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,138 @@ int wh_Bench_Mod_Aes256CTRDecryptDma(whClientContext* client,
367367

368368
#endif /* WOLFSSL_AES_COUNTER */
369369
#if defined(HAVE_AES_ECB)
370+
#if defined(WOLFHSM_CFG_DMA)
371+
static int _benchAesEcbDma(whClientContext* client, whBenchOpContext* ctx,
372+
int id, const uint8_t* key, size_t keyLen,
373+
int encrypt)
374+
{
375+
int ret = 0;
376+
int needEvict = 0;
377+
whKeyId keyId = WH_KEYID_ERASED;
378+
Aes aes[1];
379+
char keyLabel[] = "key label";
380+
const size_t inLen = WOLFHSM_CFG_BENCH_DMA_BUFFER_SIZE / 2;
381+
int i;
382+
const uint8_t* in = NULL;
383+
uint8_t* out = NULL;
384+
385+
#if defined(WOLFHSM_CFG_TEST_POSIX)
386+
/* Allocate buffers using XMALLOC with heap hints for DMA */
387+
if (ctx->transportType == WH_BENCH_TRANSPORT_POSIX_DMA) {
388+
void* heap =
389+
posixTransportShm_GetDmaHeap(client->comm->transport_context);
390+
in = XMALLOC(inLen, heap, DYNAMIC_TYPE_TMP_BUFFER);
391+
if (in == NULL) {
392+
WH_BENCH_PRINTF("Failed to allocate memory for DMA input\n");
393+
return WH_ERROR_NOSPACE;
394+
}
395+
396+
out = XMALLOC(inLen, heap, DYNAMIC_TYPE_TMP_BUFFER);
397+
if (out == NULL) {
398+
WH_BENCH_PRINTF("Failed to allocate memory for DMA output\n");
399+
XFREE((uint8_t*)in, heap, DYNAMIC_TYPE_TMP_BUFFER);
400+
return WH_ERROR_NOSPACE;
401+
}
402+
}
403+
else
404+
#endif /* WOLFHSM_CFG_TEST_POSIX */
405+
{
406+
in = WH_BENCH_DMA_BUFFER;
407+
out = (uint8_t*)in + inLen;
408+
}
409+
410+
#if defined(WOLFHSM_CFG_BENCH_INIT_DATA_BUFFERS)
411+
/* Initialize the input buffer with something non-zero */
412+
memset((uint8_t*)in, 0xAA, inLen);
413+
memset(out, 0xAA, inLen);
414+
#endif
415+
416+
/* Initialize the aes struct */
417+
ret = wc_AesInit(aes, NULL, WH_DEV_ID_DMA);
418+
if (ret != 0) {
419+
WH_BENCH_PRINTF("Failed to wc_AesInit %d\n", ret);
420+
goto exit;
421+
}
422+
423+
/* cache the key on the HSM */
424+
ret = wh_Client_KeyCache(client, WH_NVM_FLAGS_USAGE_ANY, (uint8_t*)keyLabel,
425+
sizeof(keyLabel), (uint8_t*)key, keyLen, &keyId);
426+
if (ret != 0) {
427+
WH_BENCH_PRINTF("Failed to wh_Client_KeyCache %d\n", ret);
428+
goto exit;
429+
}
430+
431+
needEvict = 1;
432+
433+
/* set the keyId on the struct */
434+
ret = wh_Client_AesSetKeyId(aes, keyId);
435+
if (ret != 0) {
436+
WH_BENCH_PRINTF("Failed to wh_Client_SetKeyIdAes %d\n", ret);
437+
goto exit;
438+
}
439+
440+
ret = wh_Bench_SetDataSize(ctx, id, inLen);
441+
if (ret != 0) {
442+
WH_BENCH_PRINTF("Failed to wh_Bench_SetDataSize %d\n", ret);
443+
goto exit;
444+
}
445+
446+
/* Perform the benchmark */
447+
for (i = 0; i < WOLFHSM_CFG_BENCH_CRYPT_ITERS; i++) {
448+
int benchStartRet;
449+
int benchStopRet;
450+
451+
if (encrypt) {
452+
benchStartRet = wh_Bench_StartOp(ctx, id);
453+
ret = wc_AesEcbEncrypt(aes, out, in, inLen);
454+
benchStopRet = wh_Bench_StopOp(ctx, id);
455+
}
456+
else {
457+
benchStartRet = wh_Bench_StartOp(ctx, id);
458+
ret = wc_AesEcbDecrypt(aes, out, in, inLen);
459+
benchStopRet = wh_Bench_StopOp(ctx, id);
460+
}
461+
462+
if (benchStartRet != 0) {
463+
WH_BENCH_PRINTF("Failed to wh_Bench_StartOp %d\n", benchStartRet);
464+
ret = benchStartRet;
465+
goto exit;
466+
}
467+
if (ret != 0) {
468+
WH_BENCH_PRINTF("Failed to wc_AesEcb%s %d\n",
469+
encrypt ? "Encrypt" : "Decrypt", ret);
470+
goto exit;
471+
}
472+
if (benchStopRet != 0) {
473+
WH_BENCH_PRINTF("Failed to wh_Bench_StopOp %d\n", benchStopRet);
474+
ret = benchStopRet;
475+
goto exit;
476+
}
477+
}
478+
479+
exit:
480+
if (needEvict) {
481+
(void)wh_Client_KeyEvict(client, keyId);
482+
}
483+
wc_AesFree(aes);
484+
485+
#if defined(WOLFHSM_CFG_TEST_POSIX)
486+
if (ctx->transportType == WH_BENCH_TRANSPORT_POSIX_DMA) {
487+
void* heap =
488+
posixTransportShm_GetDmaHeap(client->comm->transport_context);
489+
if (in != NULL) {
490+
XFREE((uint8_t*)in, heap, DYNAMIC_TYPE_TMP_BUFFER);
491+
}
492+
if (out != NULL) {
493+
XFREE(out, heap, DYNAMIC_TYPE_TMP_BUFFER);
494+
}
495+
}
496+
#endif /* WOLFHSM_CFG_TEST_POSIX */
497+
498+
return ret;
499+
}
500+
#endif /* WOLFHSM_CFG_DMA */
501+
370502
static int _benchAesEcb(whClientContext* client, whBenchOpContext* ctx, int id,
371503
const uint8_t* key, size_t keyLen, int encrypt)
372504
{
@@ -391,7 +523,7 @@ static int _benchAesEcb(whClientContext* client, whBenchOpContext* ctx, int id,
391523
ret = wc_AesInit(aes, NULL, WH_DEV_ID);
392524
if (ret != 0) {
393525
WH_BENCH_PRINTF("Failed to wc_AesInit %d\n", ret);
394-
return ret;
526+
goto exit;
395527
}
396528

397529
/* cache the key on the HSM */
@@ -497,6 +629,74 @@ int wh_Bench_Mod_Aes256ECBDecrypt(whClientContext* client,
497629
return _benchAesEcb(client, ctx, id, (uint8_t*)key256, sizeof(key256),
498630
DECRYPT);
499631
}
632+
633+
int wh_Bench_Mod_Aes128ECBEncryptDma(whClientContext* client,
634+
whBenchOpContext* ctx, int id,
635+
void* params)
636+
{
637+
#if defined(WOLFHSM_CFG_DMA)
638+
(void)params;
639+
return _benchAesEcbDma(client, ctx, id, (uint8_t*)key128, sizeof(key128),
640+
ENCRYPT);
641+
#else
642+
(void)client;
643+
(void)ctx;
644+
(void)id;
645+
(void)params;
646+
return WH_ERROR_NOTIMPL;
647+
#endif
648+
}
649+
650+
int wh_Bench_Mod_Aes128ECBDecryptDma(whClientContext* client,
651+
whBenchOpContext* ctx, int id,
652+
void* params)
653+
{
654+
#if defined(WOLFHSM_CFG_DMA)
655+
(void)params;
656+
return _benchAesEcbDma(client, ctx, id, (uint8_t*)key128, sizeof(key128),
657+
DECRYPT);
658+
#else
659+
(void)client;
660+
(void)ctx;
661+
(void)id;
662+
(void)params;
663+
return WH_ERROR_NOTIMPL;
664+
#endif
665+
}
666+
667+
int wh_Bench_Mod_Aes256ECBEncryptDma(whClientContext* client,
668+
whBenchOpContext* ctx, int id,
669+
void* params)
670+
{
671+
#if defined(WOLFHSM_CFG_DMA)
672+
(void)params;
673+
return _benchAesEcbDma(client, ctx, id, (uint8_t*)key256, sizeof(key256),
674+
ENCRYPT);
675+
#else
676+
(void)client;
677+
(void)ctx;
678+
(void)id;
679+
(void)params;
680+
return WH_ERROR_NOTIMPL;
681+
#endif
682+
}
683+
684+
int wh_Bench_Mod_Aes256ECBDecryptDma(whClientContext* client,
685+
whBenchOpContext* ctx, int id,
686+
void* params)
687+
{
688+
#if defined(WOLFHSM_CFG_DMA)
689+
(void)params;
690+
return _benchAesEcbDma(client, ctx, id, (uint8_t*)key256, sizeof(key256),
691+
DECRYPT);
692+
#else
693+
(void)client;
694+
(void)ctx;
695+
(void)id;
696+
(void)params;
697+
return WH_ERROR_NOTIMPL;
698+
#endif
699+
}
500700
#endif /* HAVE_AES_ECB */
501701

502702
#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)