Skip to content

Commit 3bf6300

Browse files
committed
AES Test improvements
1 parent 8eb4906 commit 3bf6300

2 files changed

Lines changed: 174 additions & 3 deletions

File tree

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 103
29+
#define MAX_BENCH_OPS 101
3030
/* Maximum length of operation name */
3131
#define MAX_OP_NAME 64
3232

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)