Skip to content

Commit cc0827d

Browse files
authored
Support sha2-224, 384 and 512 (#144)
* add sha2-224, 384 and 512 * addressed code review add missing Dma handling * fix Sha512 tests enable sha224, sha384 and sh512 as default enable sha224, sha384 and sha512 at tcp server * addressed code review comments * unify Sha2 Dma Request/Response addressed review comment * run clang-format
1 parent ac6bfad commit cc0827d

File tree

18 files changed

+2551
-95
lines changed

18 files changed

+2551
-95
lines changed

benchmark/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ CSTD ?= -std=c90
4747

4848
ASFLAGS ?= $(ARCHFLAGS)
4949
CFLAGS ?= $(ARCHFLAGS) $(CSTD) $(CFLAGS_EXTRA)
50-
LDFLAGS ?= $(ARCHFLAGS)
50+
LDFLAGS ?= $(ARCHFLAGS)
5151

5252
# Enable garbage collection. Inexact handling of dead_strip
5353
OS_NAME := $(shell uname -s | tr A-Z a-z)

benchmark/bench_modules/wh_bench_mod_all.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,21 @@ int wh_Bench_Mod_Sha256(whClientContext* client, whBenchOpContext* ctx, int id,
9393

9494
int wh_Bench_Mod_Sha256Dma(whClientContext* client, whBenchOpContext* ctx,
9595
int id, void* params);
96+
int wh_Bench_Mod_Sha224(whClientContext* client, whBenchOpContext* ctx, int id,
97+
void* params);
98+
99+
int wh_Bench_Mod_Sha224Dma(whClientContext* client, whBenchOpContext* ctx,
100+
int id, void* params);
101+
int wh_Bench_Mod_Sha384(whClientContext* client, whBenchOpContext* ctx, int id,
102+
void* params);
96103

104+
int wh_Bench_Mod_Sha384Dma(whClientContext* client, whBenchOpContext* ctx,
105+
int id, void* params);
106+
int wh_Bench_Mod_Sha512(whClientContext* client, whBenchOpContext* ctx, int id,
107+
void* params);
108+
109+
int wh_Bench_Mod_Sha512Dma(whClientContext* client, whBenchOpContext* ctx,
110+
int id, void* params);
97111
/*
98112
* SHA3 benchmark module prototypes (wh_bench_mod_sha3.c)
99113
*/

benchmark/bench_modules/wh_bench_mod_sha2.c

Lines changed: 363 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,367 @@ int wh_Bench_Mod_Sha256Dma(whClientContext* client, whBenchOpContext* ctx,
146146

147147
#endif /* !defined(NO_SHA256) */
148148

149+
150+
#if defined(WOLFSSL_SHA224)
151+
152+
int _benchSha224(whClientContext* client, whBenchOpContext* ctx, int id,
153+
int devId)
154+
{
155+
(void)client;
156+
157+
int ret = 0;
158+
wc_Sha224 sha224[1];
159+
uint8_t out[WC_SHA224_DIGEST_SIZE];
160+
int i = 0;
161+
int sha224Initialized = 0;
162+
const uint8_t* in;
163+
size_t inLen;
164+
165+
#if defined(WOLFHSM_CFG_DMA)
166+
if (devId == WH_DEV_ID_DMA) {
167+
in = WH_BENCH_DMA_BUFFER;
168+
inLen = WOLFHSM_CFG_BENCH_DMA_BUFFER_SIZE;
169+
}
170+
else
171+
#endif
172+
{
173+
in = WH_BENCH_DATA_IN_BUFFER;
174+
inLen = WOLFHSM_CFG_BENCH_DATA_BUFFER_SIZE;
175+
#if defined(WOLFHSM_CFG_BENCH_INIT_DATA_BUFFERS)
176+
memset(WH_BENCH_DATA_IN_BUFFER, 0xAA, inLen);
177+
#endif
178+
}
179+
180+
ret = wh_Bench_SetDataSize(ctx, id, inLen);
181+
if (ret != 0) {
182+
WH_BENCH_PRINTF("Failed to wh_Bench_SetDataSize %d\n", ret);
183+
return ret;
184+
}
185+
186+
for (i = 0; i < WOLFHSM_CFG_BENCH_CRYPT_ITERS; i++) {
187+
int benchStartRet;
188+
int benchStopRet;
189+
int initRet;
190+
int updateRet;
191+
int finalRet;
192+
193+
/* Defer error checking until after all operations are complete */
194+
benchStartRet = wh_Bench_StartOp(ctx, id);
195+
initRet = wc_InitSha224_ex(sha224, NULL, devId);
196+
updateRet = wc_Sha224Update(sha224, in, inLen);
197+
finalRet = wc_Sha224Final(sha224, out);
198+
benchStopRet = wh_Bench_StopOp(ctx, id);
199+
200+
/* Check for errors after all operations are complete */
201+
if (benchStartRet != 0) {
202+
WH_BENCH_PRINTF("Failed to wh_Bench_StartOp: %d\n", benchStartRet);
203+
ret = benchStartRet;
204+
break;
205+
}
206+
if (initRet != 0) {
207+
WH_BENCH_PRINTF("Failed to wc_InitSha224_ex %d\n", initRet);
208+
ret = initRet;
209+
break;
210+
}
211+
212+
sha224Initialized = 1;
213+
214+
if (updateRet != 0) {
215+
WH_BENCH_PRINTF("Failed to wc_Sha224Update %d\n", updateRet);
216+
ret = updateRet;
217+
break;
218+
}
219+
if (finalRet != 0) {
220+
WH_BENCH_PRINTF("Failed to wc_Sha224Final %d\n", finalRet);
221+
ret = finalRet;
222+
break;
223+
}
224+
if (benchStopRet != 0) {
225+
WH_BENCH_PRINTF("Failed to wh_Bench_StopOp: %d\n", benchStopRet);
226+
ret = benchStopRet;
227+
break;
228+
}
229+
}
230+
231+
/* Only free SHA224 if it was initialized */
232+
if (sha224Initialized) {
233+
(void)wc_Sha224Free(sha224);
234+
}
235+
236+
return ret;
237+
}
238+
239+
int wh_Bench_Mod_Sha224(whClientContext* client, whBenchOpContext* ctx, int id,
240+
void* params)
241+
{
242+
#if defined(WOLFHSM_CFG_DMA)
243+
(void)params;
244+
return _benchSha224(client, ctx, id, WH_DEV_ID);
245+
#else
246+
(void)client;
247+
(void)ctx;
248+
(void)id;
249+
(void)params;
250+
return WH_ERROR_NOTIMPL;
251+
#endif
252+
}
253+
254+
int wh_Bench_Mod_Sha224Dma(whClientContext* client, whBenchOpContext* ctx,
255+
int id, void* params)
256+
{
257+
#if defined(WOLFHSM_CFG_DMA)
258+
(void)params;
259+
return _benchSha224(client, ctx, id, WH_DEV_ID_DMA);
260+
#else
261+
(void)client;
262+
(void)ctx;
263+
(void)id;
264+
(void)params;
265+
return WH_ERROR_NOTIMPL;
266+
#endif
267+
}
268+
269+
#endif /* WOLFSSL_SHA224 */
270+
271+
#if defined(WOLFSSL_SHA384)
272+
273+
int _benchSha384(whClientContext* client, whBenchOpContext* ctx, int id,
274+
int devId)
275+
{
276+
(void)client;
277+
278+
int ret = 0;
279+
wc_Sha384 sha384[1];
280+
uint8_t out[WC_SHA384_DIGEST_SIZE];
281+
int i = 0;
282+
int sha384Initialized = 0;
283+
const uint8_t* in;
284+
size_t inLen;
285+
286+
#if defined(WOLFHSM_CFG_DMA)
287+
if (devId == WH_DEV_ID_DMA) {
288+
in = WH_BENCH_DMA_BUFFER;
289+
inLen = WOLFHSM_CFG_BENCH_DMA_BUFFER_SIZE;
290+
}
291+
else
292+
#endif
293+
{
294+
in = WH_BENCH_DATA_IN_BUFFER;
295+
inLen = WOLFHSM_CFG_BENCH_DATA_BUFFER_SIZE;
296+
#if defined(WOLFHSM_CFG_BENCH_INIT_DATA_BUFFERS)
297+
memset(WH_BENCH_DATA_IN_BUFFER, 0xAA, inLen);
298+
#endif
299+
}
300+
301+
ret = wh_Bench_SetDataSize(ctx, id, inLen);
302+
if (ret != 0) {
303+
WH_BENCH_PRINTF("Failed to wh_Bench_SetDataSize %d\n", ret);
304+
return ret;
305+
}
306+
307+
for (i = 0; i < WOLFHSM_CFG_BENCH_CRYPT_ITERS; i++) {
308+
int benchStartRet;
309+
int benchStopRet;
310+
int initRet;
311+
int updateRet;
312+
int finalRet;
313+
314+
/* Defer error checking until after all operations are complete */
315+
benchStartRet = wh_Bench_StartOp(ctx, id);
316+
initRet = wc_InitSha384_ex(sha384, NULL, devId);
317+
updateRet = wc_Sha384Update(sha384, in, inLen);
318+
finalRet = wc_Sha384Final(sha384, out);
319+
benchStopRet = wh_Bench_StopOp(ctx, id);
320+
321+
/* Check for errors after all operations are complete */
322+
if (benchStartRet != 0) {
323+
WH_BENCH_PRINTF("Failed to wh_Bench_StartOp: %d\n", benchStartRet);
324+
ret = benchStartRet;
325+
break;
326+
}
327+
if (initRet != 0) {
328+
WH_BENCH_PRINTF("Failed to wc_InitSha384_ex %d\n", initRet);
329+
ret = initRet;
330+
break;
331+
}
332+
333+
sha384Initialized = 1;
334+
335+
if (updateRet != 0) {
336+
WH_BENCH_PRINTF("Failed to wc_Sha384Update %d\n", updateRet);
337+
ret = updateRet;
338+
break;
339+
}
340+
if (finalRet != 0) {
341+
WH_BENCH_PRINTF("Failed to wc_Sha384Final %d\n", finalRet);
342+
ret = finalRet;
343+
break;
344+
}
345+
if (benchStopRet != 0) {
346+
WH_BENCH_PRINTF("Failed to wh_Bench_StopOp: %d\n", benchStopRet);
347+
ret = benchStopRet;
348+
break;
349+
}
350+
}
351+
352+
/* Only free SHA384 if it was initialized */
353+
if (sha384Initialized) {
354+
(void)wc_Sha384Free(sha384);
355+
}
356+
357+
return ret;
358+
}
359+
360+
int wh_Bench_Mod_Sha384(whClientContext* client, whBenchOpContext* ctx, int id,
361+
void* params)
362+
{
363+
#if defined(WOLFHSM_CFG_DMA)
364+
(void)params;
365+
return _benchSha384(client, ctx, id, WH_DEV_ID);
366+
#else
367+
(void)client;
368+
(void)ctx;
369+
(void)id;
370+
(void)params;
371+
return WH_ERROR_NOTIMPL;
372+
#endif
373+
}
374+
375+
int wh_Bench_Mod_Sha384Dma(whClientContext* client, whBenchOpContext* ctx,
376+
int id, void* params)
377+
{
378+
#if defined(WOLFHSM_CFG_DMA)
379+
(void)params;
380+
return _benchSha384(client, ctx, id, WH_DEV_ID_DMA);
381+
#else
382+
(void)client;
383+
(void)ctx;
384+
(void)id;
385+
(void)params;
386+
return WH_ERROR_NOTIMPL;
387+
#endif
388+
}
389+
390+
#endif /* WOLFSSL_SHA384 */
391+
#if defined(WOLFSSL_SHA512)
392+
393+
int _benchSha512(whClientContext* client, whBenchOpContext* ctx, int id,
394+
int devId)
395+
{
396+
(void)client;
397+
398+
int ret = 0;
399+
wc_Sha512 sha512[1];
400+
uint8_t out[WC_SHA512_DIGEST_SIZE];
401+
int i = 0;
402+
int sha512Initialized = 0;
403+
const uint8_t* in;
404+
size_t inLen;
405+
406+
#if defined(WOLFHSM_CFG_DMA)
407+
if (devId == WH_DEV_ID_DMA) {
408+
in = WH_BENCH_DMA_BUFFER;
409+
inLen = WOLFHSM_CFG_BENCH_DMA_BUFFER_SIZE;
410+
}
411+
else
412+
#endif
413+
{
414+
in = WH_BENCH_DATA_IN_BUFFER;
415+
inLen = WOLFHSM_CFG_BENCH_DATA_BUFFER_SIZE;
416+
#if defined(WOLFHSM_CFG_BENCH_INIT_DATA_BUFFERS)
417+
memset(WH_BENCH_DATA_IN_BUFFER, 0xAA, inLen);
418+
#endif
419+
}
420+
421+
ret = wh_Bench_SetDataSize(ctx, id, inLen);
422+
if (ret != 0) {
423+
WH_BENCH_PRINTF("Failed to wh_Bench_SetDataSize %d\n", ret);
424+
return ret;
425+
}
426+
427+
for (i = 0; i < WOLFHSM_CFG_BENCH_CRYPT_ITERS; i++) {
428+
int benchStartRet;
429+
int benchStopRet;
430+
int initRet;
431+
int updateRet;
432+
int finalRet;
433+
434+
/* Defer error checking until after all operations are complete */
435+
benchStartRet = wh_Bench_StartOp(ctx, id);
436+
initRet = wc_InitSha512_ex(sha512, NULL, devId);
437+
updateRet = wc_Sha512Update(sha512, in, inLen);
438+
finalRet = wc_Sha512Final(sha512, out);
439+
benchStopRet = wh_Bench_StopOp(ctx, id);
440+
441+
/* Check for errors after all operations are complete */
442+
if (benchStartRet != 0) {
443+
WH_BENCH_PRINTF("Failed to wh_Bench_StartOp: %d\n", benchStartRet);
444+
ret = benchStartRet;
445+
break;
446+
}
447+
if (initRet != 0) {
448+
WH_BENCH_PRINTF("Failed to wc_InitSha512_ex %d\n", initRet);
449+
ret = initRet;
450+
break;
451+
}
452+
453+
sha512Initialized = 1;
454+
455+
if (updateRet != 0) {
456+
WH_BENCH_PRINTF("Failed to wc_Sha512Update %d\n", updateRet);
457+
ret = updateRet;
458+
break;
459+
}
460+
if (finalRet != 0) {
461+
WH_BENCH_PRINTF("Failed to wc_Sha512Final %d\n", finalRet);
462+
ret = finalRet;
463+
break;
464+
}
465+
if (benchStopRet != 0) {
466+
WH_BENCH_PRINTF("Failed to wh_Bench_StopOp: %d\n", benchStopRet);
467+
ret = benchStopRet;
468+
break;
469+
}
470+
}
471+
472+
/* Only free SHA512 if it was initialized */
473+
if (sha512Initialized) {
474+
(void)wc_Sha512Free(sha512);
475+
}
476+
477+
return ret;
478+
}
479+
480+
int wh_Bench_Mod_Sha512(whClientContext* client, whBenchOpContext* ctx, int id,
481+
void* params)
482+
{
483+
#if defined(WOLFHSM_CFG_DMA)
484+
(void)params;
485+
return _benchSha512(client, ctx, id, WH_DEV_ID);
486+
#else
487+
(void)client;
488+
(void)ctx;
489+
(void)id;
490+
(void)params;
491+
return WH_ERROR_NOTIMPL;
492+
#endif
493+
}
494+
495+
int wh_Bench_Mod_Sha512Dma(whClientContext* client, whBenchOpContext* ctx,
496+
int id, void* params)
497+
{
498+
#if defined(WOLFHSM_CFG_DMA)
499+
(void)params;
500+
return _benchSha512(client, ctx, id, WH_DEV_ID_DMA);
501+
#else
502+
(void)client;
503+
(void)ctx;
504+
(void)id;
505+
(void)params;
506+
return WH_ERROR_NOTIMPL;
507+
#endif
508+
}
509+
510+
#endif /* WOLFSSL_SHA512 */
511+
149512
#endif /* WOLFHSM_CFG_BENCH_ENABLE */

0 commit comments

Comments
 (0)