@@ -39,46 +39,269 @@ static const byte key256[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
3939
4040enum { 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+
44275int 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
54283int 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
64291int 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
74299int 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 ;
0 commit comments