Skip to content

Commit 64007c0

Browse files
committed
AES Test and Benchmark improvements
1 parent b297eaa commit 64007c0

4 files changed

Lines changed: 193 additions & 15 deletions

File tree

benchmark/wh_bench.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,6 @@ typedef enum BenchModuleIdx {
259259
BENCH_MODULE_IDX_COUNT
260260
} BenchModuleIdx;
261261

262-
/* Ensure we have enough space for all modules in the context */
263-
WH_UTILS_STATIC_ASSERT(MAX_BENCH_OPS > BENCH_MODULE_IDX_COUNT,
264-
"More modules expected than MAX_BENCH_OPS");
265262

266263
/* clang-format off */
267264
static BenchModule g_benchModules[] = {
@@ -481,7 +478,7 @@ static int _runClientBenchmarks(whClientContext* client, int transport,
481478
WH_BENCH_PRINTF("Running benchmarks...\n");
482479

483480
/* Initialize benchmark context */
484-
ret = wh_Bench_Init(&benchCtx);
481+
ret = wh_Bench_Init(&benchCtx, BENCH_MODULE_IDX_COUNT);
485482
if (ret != 0) {
486483
WH_BENCH_PRINTF("Failed to initialize benchmark context: %d\n", ret);
487484
return ret;

benchmark/wh_bench_ops.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,36 @@ static uint64_t _benchGetTimeUs(void)
5454
}
5555

5656
/* Initialize benchmark context */
57-
int wh_Bench_Init(whBenchOpContext* ctx)
57+
int wh_Bench_Init(whBenchOpContext* ctx, int opMaxCount)
5858
{
5959
int i;
6060

6161
if (ctx == NULL) {
6262
return WH_ERROR_BADARGS;
6363
}
6464

65+
if (opMaxCount <= 0) {
66+
return WH_ERROR_BADARGS;
67+
}
68+
6569
/* Clear all benchmark operations */
6670
memset(ctx, 0, sizeof(whBenchOpContext));
6771

72+
ctx->ops = XMALLOC(opMaxCount * sizeof(whBenchOp), NULL,
73+
DYNAMIC_TYPE_TMP_BUFFER);
74+
if (ctx->ops == NULL) {
75+
return WH_ERROR_ABORTED;
76+
}
77+
6878
/* Initialize each operation entry */
69-
for (i = 0; i < MAX_BENCH_OPS; i++) {
79+
for (i = 0; i < opMaxCount; i++) {
7080
ctx->ops[i].valid = 0;
7181
ctx->ops[i].inProgress = 0;
7282
ctx->ops[i].minTimeUs =
7383
(uint64_t)-1; /* Set to maximum possible value */
7484
}
7585

86+
ctx->opMaxCount = opMaxCount;
7687
ctx->opCount = 0;
7788

7889
return WH_ERROR_OK;
@@ -98,7 +109,7 @@ int wh_Bench_RegisterOp(whBenchOpContext* ctx, const char* name,
98109
}
99110

100111
/* Check if we have room for a new operation */
101-
if (ctx->opCount >= MAX_BENCH_OPS) {
112+
if (ctx->opCount == ctx->opMaxCount) {
102113
return WH_ERROR_BADARGS;
103114
}
104115

benchmark/wh_bench_ops.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525

2626
#include <stdint.h>
2727

28-
/* Maximum number of operations that can be registered */
29-
#define MAX_BENCH_OPS 103
3028
/* Maximum length of operation name */
3129
#define MAX_OP_NAME 64
3230

@@ -74,17 +72,18 @@ typedef struct whBenchOp {
7472
} whBenchOp;
7573

7674
typedef struct whBenchOpContext {
77-
whBenchOp ops[MAX_BENCH_OPS]; /* Array of operations */
78-
int opCount; /* Number of registered operations */
79-
whBenchTransportType transportType; /* Type of transport */
75+
whBenchOp* ops; /* Pointer to array of operations */
76+
int opCount; /* Number of registered operations */
77+
int opMaxCount; /* Maximum number of operations */
78+
whBenchTransportType transportType; /* Type of transport */
8079
} whBenchOpContext;
8180

8281
/*
8382
* Benchmark Timing API
8483
*/
8584

8685
/* Initialize benchmark context */
87-
int wh_Bench_Init(whBenchOpContext* ctx);
86+
int wh_Bench_Init(whBenchOpContext* ctx, int opMaxCount);
8887

8988
/* Register a new benchmark operation with a name, returns ID via pointer */
9089
int wh_Bench_RegisterOp(whBenchOpContext* ctx, const char* name,

test/wh_test_crypto.c

Lines changed: 173 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3101,7 +3101,7 @@ static int whTest_NonExportableKeystore(whClientContext* ctx, int devId,
31013101
static int whTestCrypto_Aes(whClientContext* ctx, int devId, WC_RNG* rng)
31023102
{
31033103
#define WH_TEST_AES_KEYSIZE 16
3104-
#define WH_TEST_AES_TEXTSIZE 16
3104+
#define WH_TEST_AES_TEXTSIZE 64
31053105
int ret = 0;
31063106
Aes aes[1];
31073107
uint8_t iv[AES_BLOCK_SIZE];
@@ -3112,7 +3112,7 @@ static int whTestCrypto_Aes(whClientContext* ctx, int devId, WC_RNG* rng)
31123112
whKeyId keyId = WH_KEYID_ERASED;
31133113
uint8_t labelIn[WH_NVM_LABEL_LEN] = "AES Key Label";
31143114

3115-
memcpy(plainIn, PLAINTEXT, sizeof(plainIn));
3115+
memset(plainIn, 0xAA, sizeof(plainIn));
31163116

31173117
/* Randomize inputs */
31183118
ret = wc_RNG_GenerateBlock(rng, key, sizeof(key));
@@ -3236,6 +3236,119 @@ static int whTestCrypto_Aes(whClientContext* ctx, int devId, WC_RNG* rng)
32363236
WH_TEST_PRINT("AES CTR DEVID=0x%X SUCCESS\n", devId);
32373237
}
32383238
}
3239+
if (ret == 0) {
3240+
/* test aes CTR with incremental steps (block size multiple) */
3241+
ret = wc_AesInit(aes, NULL, devId);
3242+
if (ret != 0) {
3243+
WH_ERROR_PRINT("Failed to wc_AesInit %d\n", ret);
3244+
}
3245+
if (ret == WH_ERROR_OK) {
3246+
ret = wc_AesSetKeyDirect(aes, key, sizeof(key), iv, AES_ENCRYPTION);
3247+
if (ret != 0) {
3248+
WH_ERROR_PRINT("Failed to wc_AesSetKeyDirect %d\n", ret);
3249+
}
3250+
}
3251+
if (ret == WH_ERROR_OK) {
3252+
ret = wc_AesCtrEncrypt(aes, cipher, plainIn, sizeof(plainIn)/2);
3253+
if (ret != 0) {
3254+
WH_ERROR_PRINT("Failed to wc_AesCtrEncrypt %d\n", ret);
3255+
}
3256+
}
3257+
if (ret == WH_ERROR_OK) {
3258+
ret = wc_AesCtrEncrypt(aes, cipher+(sizeof(plainIn)/2),
3259+
plainIn+(sizeof(plainIn)/2),
3260+
sizeof(plainIn)/2);
3261+
if (ret != 0) {
3262+
WH_ERROR_PRINT("Failed to wc_AesCtrEncrypt %d\n", ret);
3263+
}
3264+
}
3265+
if (ret == WH_ERROR_OK) {
3266+
ret = wc_AesSetKeyDirect(aes, key, sizeof(key), iv, AES_DECRYPTION);
3267+
if (ret != 0) {
3268+
WH_ERROR_PRINT("Failed to wc_AesSetKeyDirect %d\n", ret);
3269+
}
3270+
}
3271+
if (ret == WH_ERROR_OK) {
3272+
ret = wc_AesCtrEncrypt(aes, plainOut, cipher, sizeof(cipher)/2);
3273+
if (ret != 0) {
3274+
WH_ERROR_PRINT("Failed to wc_AesCtrEncrypt %d\n", ret);
3275+
}
3276+
}
3277+
if (ret == WH_ERROR_OK) {
3278+
ret = wc_AesCtrEncrypt(aes, plainOut+sizeof(plainOut)/2,
3279+
cipher+sizeof(cipher)/2, sizeof(cipher)/2);
3280+
if (ret != 0) {
3281+
WH_ERROR_PRINT("Failed to wc_AesCtrEncrypt %d\n", ret);
3282+
}
3283+
}
3284+
if (ret == WH_ERROR_OK) {
3285+
if (memcmp(plainIn, plainOut, sizeof(plainIn)) != 0) {
3286+
WH_ERROR_PRINT("Failed to match AES-CTR\n");
3287+
ret = -1;
3288+
}
3289+
}
3290+
3291+
(void)wc_AesFree(aes);
3292+
memset(cipher, 0, sizeof(cipher));
3293+
memset(plainOut, 0, sizeof(plainOut));
3294+
}
3295+
if (ret == 0) {
3296+
/* test aes CTR with incremental steps (non block size multiple) */
3297+
ret = wc_AesInit(aes, NULL, devId);
3298+
if (ret != 0) {
3299+
WH_ERROR_PRINT("Failed to wc_AesInit %d\n", ret);
3300+
}
3301+
if (ret == WH_ERROR_OK) {
3302+
ret = wc_AesSetKeyDirect(aes, key, sizeof(key), iv, AES_ENCRYPTION);
3303+
if (ret != 0) {
3304+
WH_ERROR_PRINT("Failed to wc_AesSetKeyDirect %d\n", ret);
3305+
}
3306+
}
3307+
if (ret == WH_ERROR_OK) {
3308+
ret = wc_AesCtrEncrypt(aes, cipher, plainIn, (sizeof(plainIn)/2)-1);
3309+
if (ret != 0) {
3310+
WH_ERROR_PRINT("Failed to wc_AesCtrEncrypt %d\n", ret);
3311+
}
3312+
}
3313+
if (ret == WH_ERROR_OK) {
3314+
ret = wc_AesCtrEncrypt(aes, cipher+((sizeof(plainIn)/2)-1),
3315+
plainIn+((sizeof(plainIn)/2)-1),
3316+
(sizeof(plainIn)/2)+1);
3317+
if (ret != 0) {
3318+
WH_ERROR_PRINT("Failed to wc_AesCtrEncrypt %d\n", ret);
3319+
}
3320+
}
3321+
if (ret == WH_ERROR_OK) {
3322+
ret = wc_AesSetKeyDirect(aes, key, sizeof(key), iv, AES_DECRYPTION);
3323+
if (ret != 0) {
3324+
WH_ERROR_PRINT("Failed to wc_AesSetKeyDirect %d\n", ret);
3325+
}
3326+
}
3327+
if (ret == WH_ERROR_OK) {
3328+
ret = wc_AesCtrEncrypt(aes, plainOut, cipher, (sizeof(cipher)/2)+1);
3329+
if (ret != 0) {
3330+
WH_ERROR_PRINT("Failed to wc_AesCtrEncrypt %d\n", ret);
3331+
}
3332+
}
3333+
if (ret == WH_ERROR_OK) {
3334+
ret = wc_AesCtrEncrypt(aes, plainOut+((sizeof(plainOut)/2)+1),
3335+
cipher+((sizeof(cipher)/2)+1),
3336+
(sizeof(cipher)/2)-1);
3337+
if (ret != 0) {
3338+
WH_ERROR_PRINT("Failed to wc_AesCtrEncrypt %d\n", ret);
3339+
}
3340+
}
3341+
if (ret == WH_ERROR_OK) {
3342+
if (memcmp(plainIn, plainOut, sizeof(plainIn)) != 0) {
3343+
WH_ERROR_PRINT("Failed to match AES-CTR\n");
3344+
ret = -1;
3345+
}
3346+
}
3347+
3348+
(void)wc_AesFree(aes);
3349+
memset(cipher, 0, sizeof(cipher));
3350+
memset(plainOut, 0, sizeof(plainOut));
3351+
}
32393352
#endif
32403353

32413354
#ifdef HAVE_AES_ECB
@@ -3351,6 +3464,8 @@ static int whTestCrypto_Aes(whClientContext* ctx, int devId, WC_RNG* rng)
33513464
WH_TEST_PRINT("AES ECB DEVID=0x%X SUCCESS\n", devId);
33523465
}
33533466
}
3467+
/* AES ECB doesn't need a test with incremental steps as each block is
3468+
* processed independently. */
33543469
#endif /* HAVE_AES_ECB */
33553470

33563471
#ifdef HAVE_AES_CBC
@@ -3453,6 +3568,62 @@ static int whTestCrypto_Aes(whClientContext* ctx, int devId, WC_RNG* rng)
34533568
WH_TEST_PRINT("AES CBC DEVID=0x%X SUCCESS\n", devId);
34543569
}
34553570
}
3571+
if (ret == 0) {
3572+
/* test aes CBC with incremental steps (block size multiple) */
3573+
ret = wc_AesInit(aes, NULL, devId);
3574+
if (ret != 0) {
3575+
WH_ERROR_PRINT("Failed to wc_AesInit %d\n", ret);
3576+
}
3577+
if (ret == WH_ERROR_OK) {
3578+
ret = wc_AesSetKey(aes, key, sizeof(key), iv, AES_ENCRYPTION);
3579+
if (ret != 0) {
3580+
WH_ERROR_PRINT("Failed to wc_AesSetKey %d\n", ret);
3581+
}
3582+
}
3583+
if (ret == WH_ERROR_OK) {
3584+
ret = wc_AesCbcEncrypt(aes, cipher, plainIn, sizeof(plainIn)/2);
3585+
if (ret != 0) {
3586+
WH_ERROR_PRINT("Failed to wc_AesCbcEncrypt %d\n", ret);
3587+
}
3588+
}
3589+
if (ret == WH_ERROR_OK) {
3590+
ret = wc_AesCbcEncrypt(aes, cipher+(sizeof(plainIn)/2),
3591+
plainIn+(sizeof(plainIn)/2),
3592+
sizeof(plainIn)/2);
3593+
if (ret != 0) {
3594+
WH_ERROR_PRINT("Failed to wc_AesCbcEncrypt %d\n", ret);
3595+
}
3596+
}
3597+
if (ret == WH_ERROR_OK) {
3598+
ret = wc_AesSetKey(aes, key, sizeof(key), iv, AES_DECRYPTION);
3599+
if (ret != 0) {
3600+
WH_ERROR_PRINT("Failed to wc_AesSetKey %d\n", ret);
3601+
}
3602+
}
3603+
if (ret == WH_ERROR_OK) {
3604+
ret = wc_AesCbcDecrypt(aes, plainOut, cipher, sizeof(cipher)/2);
3605+
if (ret != 0) {
3606+
WH_ERROR_PRINT("Failed to wc_AesCbcEncrypt %d\n", ret);
3607+
}
3608+
}
3609+
if (ret == WH_ERROR_OK) {
3610+
ret = wc_AesCbcDecrypt(aes, plainOut+sizeof(plainOut)/2,
3611+
cipher+sizeof(cipher)/2, sizeof(cipher)/2);
3612+
if (ret != 0) {
3613+
WH_ERROR_PRINT("Failed to wc_AesCbcEncrypt %d\n", ret);
3614+
}
3615+
}
3616+
if (ret == WH_ERROR_OK) {
3617+
if (memcmp(plainIn, plainOut, sizeof(plainIn)) != 0) {
3618+
WH_ERROR_PRINT("Failed to match AES-CBC\n");
3619+
ret = -1;
3620+
}
3621+
}
3622+
3623+
(void)wc_AesFree(aes);
3624+
memset(cipher, 0, sizeof(cipher));
3625+
memset(plainOut, 0, sizeof(plainOut));
3626+
}
34563627
#endif /* HAVE_AES_CBC */
34573628

34583629
#ifdef HAVE_AESGCM

0 commit comments

Comments
 (0)