Skip to content

Commit 0f52794

Browse files
authored
Merge pull request #155 from miyazakh/add_aes_ctr
Support AES-CTR and AES-ECB
2 parents 618913f + 689fcb8 commit 0f52794

File tree

16 files changed

+1139
-41
lines changed

16 files changed

+1139
-41
lines changed

benchmark/bench_modules/wh_bench_mod_aes.c

Lines changed: 239 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,46 +39,269 @@ static const byte key256[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
3939

4040
enum { DECRYPT = 0, ENCRYPT = 1 };
4141

42+
#if defined(WOLFSSL_AES_COUNTER)
43+
static int _benchAesCtr(whClientContext* client, whBenchOpContext* ctx, int id,
44+
const uint8_t* key, size_t keyLen, int encrypt)
45+
{
46+
int ret = 0;
47+
int needEvict = 0;
48+
whKeyId keyId = WH_KEYID_ERASED;
49+
Aes aes[1];
50+
char keyLabel[] = "key label";
51+
/* Input size is largest multiple of AES block size that fits in buffer */
52+
const size_t inLen =
53+
(WOLFHSM_CFG_BENCH_DATA_BUFFER_SIZE / WC_AES_BLOCK_SIZE) *
54+
WC_AES_BLOCK_SIZE;
55+
int i;
56+
57+
#if defined(WOLFHSM_CFG_BENCH_INIT_DATA_BUFFERS)
58+
/* Initialize the input buffer with something non-zero */
59+
memset(WH_BENCH_DATA_IN_BUFFER, 0xAA, inLen);
60+
memset(WH_BENCH_DATA_OUT_BUFFER, 0xAA, inLen);
61+
#endif
62+
63+
/* Initialize the aes struct */
64+
ret = wc_AesInit(aes, NULL, WH_DEV_ID);
65+
if (ret != 0) {
66+
WH_BENCH_PRINTF("Failed to wc_AesInit %d\n", ret);
67+
return ret;
68+
}
4269

70+
/* cache the key on the HSM */
71+
ret = wh_Client_KeyCache(client, 0, (uint8_t*)keyLabel, sizeof(keyLabel),
72+
(uint8_t*)key, keyLen, &keyId);
73+
if (ret != 0) {
74+
WH_BENCH_PRINTF("Failed to wh_Client_KeyCache %d\n", ret);
75+
goto exit;
76+
}
77+
78+
needEvict = 1;
79+
80+
/* set the keyId on the struct */
81+
ret = wh_Client_AesSetKeyId(aes, keyId);
82+
if (ret != 0) {
83+
WH_BENCH_PRINTF("Failed to wh_Client_SetKeyIdAes %d\n", ret);
84+
goto exit;
85+
}
86+
87+
ret = wh_Bench_SetDataSize(ctx, id, inLen);
88+
if (ret != 0) {
89+
WH_BENCH_PRINTF("Failed to wh_Bench_SetDataSize %d\n", ret);
90+
goto exit;
91+
}
92+
93+
for (i = 0; i < WOLFHSM_CFG_BENCH_CRYPT_ITERS; i++) {
94+
int benchStartRet;
95+
int benchStopRet;
96+
97+
if (encrypt) {
98+
benchStartRet = wh_Bench_StartOp(ctx, id);
99+
ret = wc_AesCtrEncrypt(aes, WH_BENCH_DATA_OUT_BUFFER,
100+
WH_BENCH_DATA_IN_BUFFER, inLen);
101+
benchStopRet = wh_Bench_StopOp(ctx, id);
102+
}
103+
else {
104+
benchStartRet = wh_Bench_StartOp(ctx, id);
105+
ret = wc_AesCtrEncrypt(aes, WH_BENCH_DATA_OUT_BUFFER,
106+
WH_BENCH_DATA_IN_BUFFER, inLen);
107+
benchStopRet = wh_Bench_StopOp(ctx, id);
108+
}
109+
110+
if (benchStartRet != 0) {
111+
WH_BENCH_PRINTF("Failed to wh_Bench_StartOp %d\n", benchStartRet);
112+
ret = benchStartRet;
113+
goto exit;
114+
}
115+
if (ret != 0) {
116+
WH_BENCH_PRINTF("Failed to wc_AesCtrc%s %d\n",
117+
encrypt ? "Encrypt" : "Decrypt", ret);
118+
goto exit;
119+
}
120+
if (benchStopRet != 0) {
121+
WH_BENCH_PRINTF("Failed to wh_Bench_StopOp %d\n", benchStopRet);
122+
ret = benchStopRet;
123+
goto exit;
124+
}
125+
}
126+
127+
exit:
128+
wc_AesFree(aes);
129+
130+
if (needEvict) {
131+
int evictRet = wh_Client_KeyEvict(client, keyId);
132+
if (evictRet != 0) {
133+
WH_BENCH_PRINTF("Failed to wh_Client_KeyEvict %d\n", evictRet);
134+
if (ret == 0) {
135+
ret = evictRet;
136+
}
137+
}
138+
}
139+
return ret;
140+
}
141+
142+
int wh_Bench_Mod_Aes128CTREncrypt(whClientContext* client,
143+
whBenchOpContext* ctx, int id, void* params)
144+
{
145+
(void)params;
146+
return _benchAesCtr(client, ctx, id, (uint8_t*)key128, sizeof(key128),
147+
ENCRYPT);
148+
}
149+
150+
int wh_Bench_Mod_Aes128CTRDecrypt(whClientContext* client,
151+
whBenchOpContext* ctx, int id, void* params)
152+
{
153+
(void)params;
154+
return _benchAesCtr(client, ctx, id, (uint8_t*)key128, sizeof(key128),
155+
DECRYPT);
156+
}
157+
158+
int wh_Bench_Mod_Aes256CTREncrypt(whClientContext* client,
159+
whBenchOpContext* ctx, int id, void* params)
160+
{
161+
(void)params;
162+
return _benchAesCtr(client, ctx, id, (uint8_t*)key256, sizeof(key256),
163+
ENCRYPT);
164+
}
165+
166+
int wh_Bench_Mod_Aes256CTRDecrypt(whClientContext* client,
167+
whBenchOpContext* ctx, int id, void* params)
168+
{
169+
(void)params;
170+
return _benchAesCtr(client, ctx, id, (uint8_t*)key256, sizeof(key256),
171+
DECRYPT);
172+
}
173+
174+
#endif /* WOLFSSL_AES_COUNTER */
43175
#if defined(HAVE_AES_ECB)
176+
static int _benchAesEcb(whClientContext* client, whBenchOpContext* ctx, int id,
177+
const uint8_t* key, size_t keyLen, int encrypt)
178+
{
179+
int ret = 0;
180+
int needEvict = 0;
181+
whKeyId keyId = WH_KEYID_ERASED;
182+
Aes aes[1];
183+
char keyLabel[] = "key label";
184+
/* Input size is largest multiple of AES block size that fits in buffer */
185+
const size_t inLen =
186+
(WOLFHSM_CFG_BENCH_DATA_BUFFER_SIZE / WC_AES_BLOCK_SIZE) *
187+
WC_AES_BLOCK_SIZE;
188+
int i;
189+
190+
#if defined(WOLFHSM_CFG_BENCH_INIT_DATA_BUFFERS)
191+
/* Initialize the input buffer with something non-zero */
192+
memset(WH_BENCH_DATA_IN_BUFFER, 0xAA, inLen);
193+
memset(WH_BENCH_DATA_OUT_BUFFER, 0xAA, inLen);
194+
#endif
195+
196+
/* Initialize the aes struct */
197+
ret = wc_AesInit(aes, NULL, WH_DEV_ID);
198+
if (ret != 0) {
199+
WH_BENCH_PRINTF("Failed to wc_AesInit %d\n", ret);
200+
return ret;
201+
}
202+
203+
/* cache the key on the HSM */
204+
ret = wh_Client_KeyCache(client, 0, (uint8_t*)keyLabel, sizeof(keyLabel),
205+
(uint8_t*)key, keyLen, &keyId);
206+
if (ret != 0) {
207+
WH_BENCH_PRINTF("Failed to wh_Client_KeyCache %d\n", ret);
208+
goto exit;
209+
}
210+
211+
needEvict = 1;
212+
213+
/* set the keyId on the struct */
214+
ret = wh_Client_AesSetKeyId(aes, keyId);
215+
if (ret != 0) {
216+
WH_BENCH_PRINTF("Failed to wh_Client_SetKeyIdAes %d\n", ret);
217+
goto exit;
218+
}
219+
220+
ret = wh_Bench_SetDataSize(ctx, id, inLen);
221+
if (ret != 0) {
222+
WH_BENCH_PRINTF("Failed to wh_Bench_SetDataSize %d\n", ret);
223+
goto exit;
224+
}
225+
226+
for (i = 0; i < WOLFHSM_CFG_BENCH_CRYPT_ITERS; i++) {
227+
int benchStartRet;
228+
int benchStopRet;
229+
230+
if (encrypt) {
231+
benchStartRet = wh_Bench_StartOp(ctx, id);
232+
ret = wc_AesEcbEncrypt(aes, WH_BENCH_DATA_OUT_BUFFER,
233+
WH_BENCH_DATA_IN_BUFFER, inLen);
234+
benchStopRet = wh_Bench_StopOp(ctx, id);
235+
}
236+
else {
237+
benchStartRet = wh_Bench_StartOp(ctx, id);
238+
ret = wc_AesEcbDecrypt(aes, WH_BENCH_DATA_OUT_BUFFER,
239+
WH_BENCH_DATA_IN_BUFFER, inLen);
240+
benchStopRet = wh_Bench_StopOp(ctx, id);
241+
}
242+
243+
if (benchStartRet != 0) {
244+
WH_BENCH_PRINTF("Failed to wh_Bench_StartOp %d\n", benchStartRet);
245+
ret = benchStartRet;
246+
goto exit;
247+
}
248+
if (ret != 0) {
249+
WH_BENCH_PRINTF("Failed to wc_AesEcb%s %d\n",
250+
encrypt ? "Encrypt" : "Decrypt", ret);
251+
goto exit;
252+
}
253+
if (benchStopRet != 0) {
254+
WH_BENCH_PRINTF("Failed to wh_Bench_StopOp %d\n", benchStopRet);
255+
ret = benchStopRet;
256+
goto exit;
257+
}
258+
}
259+
260+
exit:
261+
wc_AesFree(aes);
262+
263+
if (needEvict) {
264+
int evictRet = wh_Client_KeyEvict(client, keyId);
265+
if (evictRet != 0) {
266+
WH_BENCH_PRINTF("Failed to wh_Client_KeyEvict %d\n", evictRet);
267+
if (ret == 0) {
268+
ret = evictRet;
269+
}
270+
}
271+
}
272+
return ret;
273+
}
274+
44275
int wh_Bench_Mod_Aes128ECBEncrypt(whClientContext* client,
45276
whBenchOpContext* ctx, int id, void* params)
46277
{
47-
(void)client;
48-
(void)ctx;
49-
(void)id;
50278
(void)params;
51-
return WH_ERROR_NOTIMPL;
279+
return _benchAesEcb(client, ctx, id, (uint8_t*)key128, sizeof(key128),
280+
ENCRYPT);
52281
}
53282

54283
int wh_Bench_Mod_Aes128ECBDecrypt(whClientContext* client,
55284
whBenchOpContext* ctx, int id, void* params)
56285
{
57-
(void)client;
58-
(void)ctx;
59-
(void)id;
60286
(void)params;
61-
return WH_ERROR_NOTIMPL;
287+
return _benchAesEcb(client, ctx, id, (uint8_t*)key128, sizeof(key128),
288+
DECRYPT);
62289
}
63290

64291
int wh_Bench_Mod_Aes256ECBEncrypt(whClientContext* client,
65292
whBenchOpContext* ctx, int id, void* params)
66293
{
67-
(void)client;
68-
(void)ctx;
69-
(void)id;
70294
(void)params;
71-
return WH_ERROR_NOTIMPL;
295+
return _benchAesEcb(client, ctx, id, (uint8_t*)key256, sizeof(key256),
296+
ENCRYPT);
72297
}
73298

74299
int wh_Bench_Mod_Aes256ECBDecrypt(whClientContext* client,
75300
whBenchOpContext* ctx, int id, void* params)
76301
{
77-
(void)client;
78-
(void)ctx;
79-
(void)id;
80302
(void)params;
81-
return WH_ERROR_NOTIMPL;
303+
return _benchAesEcb(client, ctx, id, (uint8_t*)key256, sizeof(key256),
304+
DECRYPT);
82305
}
83306
#endif /* HAVE_AES_ECB */
84307

@@ -92,7 +315,6 @@ static int _benchAesCbc(whClientContext* client, whBenchOpContext* ctx, int id,
92315
Aes aes[1];
93316
char keyLabel[] = "key label";
94317
/* Input size is largest multiple of AES block size that fits in buffer */
95-
/* BUFFER-TODO */
96318
const size_t inLen =
97319
(WOLFHSM_CFG_BENCH_DATA_BUFFER_SIZE / WC_AES_BLOCK_SIZE) *
98320
WC_AES_BLOCK_SIZE;

benchmark/bench_modules/wh_bench_mod_all.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ int wh_Bench_Mod_Echo(whClientContext* client, whBenchOpContext* benchCtx,
2828
/*
2929
* AES benchmark module prototypes (wh_bench_mod_aes.c)
3030
*/
31+
int wh_Bench_Mod_Aes128CTREncrypt(whClientContext* client,
32+
whBenchOpContext* ctx, int id, void* params);
33+
34+
int wh_Bench_Mod_Aes128CTRDecrypt(whClientContext* client,
35+
whBenchOpContext* ctx, int id, void* params);
3136
int wh_Bench_Mod_Aes128ECBEncrypt(whClientContext* client,
3237
whBenchOpContext* ctx, int id, void* params);
3338

@@ -46,6 +51,12 @@ int wh_Bench_Mod_Aes128GCMEncrypt(whClientContext* client,
4651
int wh_Bench_Mod_Aes128GCMDecrypt(whClientContext* client,
4752
whBenchOpContext* ctx, int id, void* params);
4853

54+
int wh_Bench_Mod_Aes256CTREncrypt(whClientContext* client,
55+
whBenchOpContext* ctx, int id, void* params);
56+
57+
int wh_Bench_Mod_Aes256CTRDecrypt(whClientContext* client,
58+
whBenchOpContext* ctx, int id, void* params);
59+
4960
int wh_Bench_Mod_Aes256ECBEncrypt(whClientContext* client,
5061
whBenchOpContext* ctx, int id, void* params);
5162

benchmark/config/user_settings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ extern "C" {
118118
/** AES Options */
119119
/* #define NO_AES */
120120
#define HAVE_AESGCM
121+
#define WOLFSSL_AES_COUNTER
121122
#define GCM_TABLE_4BIT
122123

123124
#define WOLFSSL_AES_DIRECT

benchmark/wh_bench.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ typedef enum BenchModuleIdx {
7575

7676
/* AES */
7777
#if !defined(NO_AES)
78+
#if defined(WOLFSSL_AES_COUNTER)
79+
BENCH_MODULE_IDX_AES_128_CTR_ENCRYPT,
80+
BENCH_MODULE_IDX_AES_128_CTR_DECRYPT,
81+
BENCH_MODULE_IDX_AES_256_CTR_ENCRYPT,
82+
BENCH_MODULE_IDX_AES_256_CTR_DECRYPT,
83+
#endif /* WOLFSSL_AES_COUNTER */
7884
#if defined(HAVE_AES_ECB)
7985
BENCH_MODULE_IDX_AES_128_ECB_ENCRYPT,
8086
BENCH_MODULE_IDX_AES_128_ECB_DECRYPT,
@@ -233,6 +239,12 @@ static BenchModule g_benchModules[] = {
233239

234240
/* AES */
235241
#if !defined(NO_AES)
242+
#if defined(WOLFSSL_AES_COUNTER)
243+
[BENCH_MODULE_IDX_AES_128_CTR_ENCRYPT] = {"AES-128-CTR-Encrypt", wh_Bench_Mod_Aes128CTREncrypt, BENCH_THROUGHPUT_XBPS, 0, NULL},
244+
[BENCH_MODULE_IDX_AES_128_CTR_DECRYPT] = {"AES-128-CTR-Decrypt", wh_Bench_Mod_Aes128CTRDecrypt, BENCH_THROUGHPUT_XBPS, 0, NULL},
245+
[BENCH_MODULE_IDX_AES_256_CTR_ENCRYPT] = {"AES-256-CTR-Encrypt", wh_Bench_Mod_Aes256CTREncrypt, BENCH_THROUGHPUT_XBPS, 0, NULL},
246+
[BENCH_MODULE_IDX_AES_256_CTR_DECRYPT] = {"AES-256-CTR-Decrypt", wh_Bench_Mod_Aes256CTRDecrypt, BENCH_THROUGHPUT_XBPS, 0, NULL},
247+
#endif /* WOLFSSL_AES_COUNTER */
236248
#if defined(HAVE_AES_ECB)
237249
[BENCH_MODULE_IDX_AES_128_ECB_ENCRYPT] = {"AES-128-ECB-Encrypt", wh_Bench_Mod_Aes128ECBEncrypt, BENCH_THROUGHPUT_XBPS, 0, NULL},
238250
[BENCH_MODULE_IDX_AES_128_ECB_DECRYPT] = {"AES-128-ECB-Decrypt", wh_Bench_Mod_Aes128ECBDecrypt, 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 79
29+
#define MAX_BENCH_OPS 83
3030
/* Maximum length of operation name */
3131
#define MAX_OP_NAME 64
3232

examples/posix/tcp/wh_client_tcp/user_settings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#define HAVE_CURVE25519
3939
#define HAVE_ECC
4040
#define HAVE_AES_CBC
41+
#define WOLFSSL_AES_COUNTER
4142
#define HAVE_AESGCM
4243
#define WOLFSSL_AES_DIRECT
4344
#define WOLFSSL_CMAC

examples/posix/tcp/wh_server_tcp/user_settings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ extern "C" {
107107
/** AES Options */
108108
/* #define NO_AES */
109109
#define HAVE_AESGCM
110+
#define WOLFSSL_AES_COUNTER
110111
#define GCM_TABLE_4BIT
111112

112113
#define WOLFSSL_AES_DIRECT

0 commit comments

Comments
 (0)