Skip to content

Commit 14ebc6c

Browse files
add return value checks, free in benchmark buffer on error, add macro guards
1 parent b9cc67e commit 14ebc6c

3 files changed

Lines changed: 52 additions & 19 deletions

File tree

benchmark/bench_modules/wh_bench_mod_cmac.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
6767
#if defined(WOLFHSM_CFG_DMA)
6868
if (devId == WH_DEV_ID_DMA) {
6969
inLen = WOLFHSM_CFG_BENCH_DMA_BUFFER_SIZE;
70+
#if defined(WOLFHSM_CFG_TEST_POSIX)
7071
if (ctx->transportType == WH_BENCH_TRANSPORT_POSIX_DMA) {
7172
/* if static memory was used with DMA then use XMALLOC */
7273
void* heap =
@@ -80,12 +81,14 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
8081
DYNAMIC_TYPE_TMP_BUFFER);
8182
if (out == NULL) {
8283
WH_BENCH_PRINTF("Failed to allocate memory for DMA\n");
84+
XFREE(in, heap, DYNAMIC_TYPE_TMP_BUFFER);
8385
return WH_ERROR_NOSPACE;
8486
}
8587
}
8688
else {
8789
in = WH_BENCH_DMA_BUFFER;
8890
}
91+
#endif /* WOLFHSM_CFG_TEST_POSIX */
8992
}
9093
else
9194
#endif
@@ -157,6 +160,7 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
157160
}
158161
}
159162
#if defined(WOLFHSM_CFG_DMA)
163+
#if defined(WOLFHSM_CFG_TEST_POSIX)
160164
if (devId == WH_DEV_ID_DMA &&
161165
ctx->transportType == WH_BENCH_TRANSPORT_POSIX_DMA) {
162166
/* if static memory was used with DMA then use XFREE */
@@ -165,6 +169,7 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
165169
XFREE(in, heap, DYNAMIC_TYPE_TMP_BUFFER);
166170
XFREE(out, heap, DYNAMIC_TYPE_TMP_BUFFER);
167171
}
172+
#endif /* WOLFHSM_CFG_TEST_POSIX */
168173
#endif
169174
(void)wc_CmacFree(cmac);
170175
return ret;

benchmark/bench_modules/wh_bench_mod_sha2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ int _benchSha256(whClientContext* client, whBenchOpContext* ctx, int id,
5353
#if defined(WOLFHSM_CFG_DMA)
5454
if (devId == WH_DEV_ID_DMA) {
5555
inLen = WOLFHSM_CFG_BENCH_DMA_BUFFER_SIZE;
56-
5756
#if defined(WOLFHSM_CFG_TEST_POSIX)
5857
if (ctx->transportType == WH_BENCH_TRANSPORT_POSIX_DMA) {
5958
/* if static memory was used with DMA then use XMALLOC */
@@ -67,6 +66,7 @@ int _benchSha256(whClientContext* client, whBenchOpContext* ctx, int id,
6766
out = XMALLOC(WC_SHA256_DIGEST_SIZE, heap, DYNAMIC_TYPE_TMP_BUFFER);
6867
if (out == NULL) {
6968
WH_BENCH_PRINTF("Failed to allocate memory for DMA\n");
69+
XFREE((uint8_t*)in, heap, DYNAMIC_TYPE_TMP_BUFFER);
7070
return WH_ERROR_NOSPACE;
7171
}
7272
}

src/wh_client_crypto.c

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2910,7 +2910,9 @@ int wh_Client_CmacDma(whClientContext* ctx, Cmac* cmac, CmacType type,
29102910
ret = wh_Client_DmaProcessClientAddress(
29112911
ctx, (uintptr_t)cmac, (void**)&stateAddr, req->state.sz,
29122912
WH_DMA_OPER_CLIENT_READ_PRE, (whDmaFlags){0});
2913-
req->state.addr = stateAddr;
2913+
if (ret == WH_ERROR_OK) {
2914+
req->state.addr = stateAddr;
2915+
}
29142916

29152917
/* Handle different CMAC operations based on input parameters */
29162918
if (ret == WH_ERROR_OK && key != NULL) {
@@ -2919,7 +2921,9 @@ int wh_Client_CmacDma(whClientContext* ctx, Cmac* cmac, CmacType type,
29192921
ret = wh_Client_DmaProcessClientAddress(
29202922
ctx, (uintptr_t)key, (void**)&keyAddr, req->key.sz,
29212923
WH_DMA_OPER_CLIENT_READ_PRE, (whDmaFlags){0});
2922-
req->key.addr = keyAddr;
2924+
if (ret == WH_ERROR_OK) {
2925+
req->key.addr = keyAddr;
2926+
}
29232927
}
29242928

29252929
if (ret == WH_ERROR_OK && in != NULL) {
@@ -3195,20 +3199,26 @@ int wh_Client_Sha256Dma(whClientContext* ctx, wc_Sha256* sha, const uint8_t* in,
31953199
ret = wh_Client_DmaProcessClientAddress(
31963200
ctx, (uintptr_t)sha256, (void**)&stateAddr, req->state.sz,
31973201
WH_DMA_OPER_CLIENT_WRITE_PRE, (whDmaFlags){0});
3198-
req->state.addr = stateAddr;
3202+
if (ret == WH_ERROR_OK) {
3203+
req->state.addr = stateAddr;
3204+
}
31993205

32003206
if (ret == WH_ERROR_OK) {
32013207
ret = wh_Client_DmaProcessClientAddress(
32023208
ctx, (uintptr_t)in, (void**)&inAddr, req->input.sz,
32033209
WH_DMA_OPER_CLIENT_READ_PRE, (whDmaFlags){0});
3204-
req->input.addr = inAddr;
3210+
if (ret == WH_ERROR_OK) {
3211+
req->input.addr = inAddr;
3212+
}
32053213
}
32063214

32073215
if (ret == WH_ERROR_OK) {
32083216
ret = wh_Client_DmaProcessClientAddress(
32093217
ctx, (uintptr_t)out, (void**)&outAddr, req->output.sz,
32103218
WH_DMA_OPER_CLIENT_WRITE_PRE, (whDmaFlags){0});
3211-
req->output.addr = outAddr;
3219+
if (ret == WH_ERROR_OK) {
3220+
req->output.addr = outAddr;
3221+
}
32123222
}
32133223
}
32143224

@@ -3483,26 +3493,32 @@ int wh_Client_Sha224Dma(whClientContext* ctx, wc_Sha224* sha, const uint8_t* in,
34833493
ret = wh_Client_DmaProcessClientAddress(
34843494
ctx, (uintptr_t)sha224, (void**)&stateAddr, req->state.sz,
34853495
WH_DMA_OPER_CLIENT_WRITE_PRE, (whDmaFlags){0});
3486-
req->state.addr = stateAddr;
3496+
if (ret == WH_ERROR_OK) {
3497+
req->state.addr = stateAddr;
3498+
}
34873499

34883500
if (ret == WH_ERROR_OK) {
34893501
ret = wh_Client_DmaProcessClientAddress(
34903502
ctx, (uintptr_t)in, (void**)&inAddr, req->input.sz,
34913503
WH_DMA_OPER_CLIENT_READ_PRE, (whDmaFlags){0});
3492-
req->input.addr = inAddr;
3504+
if (ret == WH_ERROR_OK) {
3505+
req->input.addr = inAddr;
3506+
}
34933507
}
34943508

34953509
if (ret == WH_ERROR_OK) {
34963510
ret = wh_Client_DmaProcessClientAddress(
34973511
ctx, (uintptr_t)out, (void**)&outAddr, req->output.sz,
34983512
WH_DMA_OPER_CLIENT_WRITE_PRE, (whDmaFlags){0});
3499-
req->output.addr = outAddr;
3513+
if (ret == WH_ERROR_OK) {
3514+
req->output.addr = outAddr;
3515+
}
35003516
}
35013517
}
35023518

35033519
/* Caller invoked SHA Update:
35043520
* wc_CryptoCb_Sha224Hash(sha224, data, len, NULL) */
3505-
if (in != NULL) {
3521+
if (in != NULL && ret == WH_ERROR_OK) {
35063522
req->finalize = 0;
35073523

35083524
#ifdef DEBUG_CRYPTOCB_VERBOSE
@@ -3768,26 +3784,32 @@ int wh_Client_Sha384Dma(whClientContext* ctx, wc_Sha384* sha, const uint8_t* in,
37683784
ret = wh_Client_DmaProcessClientAddress(
37693785
ctx, (uintptr_t)sha384, (void**)&stateAddr, req->state.sz,
37703786
WH_DMA_OPER_CLIENT_WRITE_PRE, (whDmaFlags){0});
3771-
req->state.addr = stateAddr;
3787+
if (ret == WH_ERROR_OK) {
3788+
req->state.addr = stateAddr;
3789+
}
37723790

37733791
if (ret == WH_ERROR_OK) {
37743792
ret = wh_Client_DmaProcessClientAddress(
37753793
ctx, (uintptr_t)in, (void**)&inAddr, req->input.sz,
37763794
WH_DMA_OPER_CLIENT_READ_PRE, (whDmaFlags){0});
3777-
req->input.addr = inAddr;
3795+
if (ret == WH_ERROR_OK) {
3796+
req->input.addr = inAddr;
3797+
}
37783798
}
37793799

37803800
if (ret == WH_ERROR_OK) {
37813801
ret = wh_Client_DmaProcessClientAddress(
37823802
ctx, (uintptr_t)out, (void**)&outAddr, req->output.sz,
37833803
WH_DMA_OPER_CLIENT_WRITE_PRE, (whDmaFlags){0});
3784-
req->output.addr = outAddr;
3804+
if (ret == WH_ERROR_OK) {
3805+
req->output.addr = outAddr;
3806+
}
37853807
}
37863808
}
37873809

37883810
/* Caller invoked SHA Update:
37893811
* wc_CryptoCb_Sha384Hash(sha384, data, len, NULL) */
3790-
if (in != NULL) {
3812+
if (in != NULL && ret == WH_ERROR_OK) {
37913813
req->finalize = 0;
37923814
#ifdef DEBUG_CRYPTOCB_VERBOSE
37933815
printf("[client] SHA384 DMA UPDATE: inAddr=%p, inSz=%u\n", in, inLen);
@@ -3860,7 +3882,7 @@ int wh_Client_Sha384Dma(whClientContext* ctx, wc_Sha384* sha, const uint8_t* in,
38603882
}
38613883
return ret;
38623884
}
3863-
#endif /* WOLHSM_CFG_DMA */
3885+
#endif /* WOLFHSM_CFG_DMA */
38643886
#endif /* WOLFSSL_SHA384 */
38653887

38663888

@@ -4064,26 +4086,32 @@ int wh_Client_Sha512Dma(whClientContext* ctx, wc_Sha512* sha, const uint8_t* in,
40644086
ret = wh_Client_DmaProcessClientAddress(
40654087
ctx, (uintptr_t)sha512, (void**)&stateAddr, req->state.sz,
40664088
WH_DMA_OPER_CLIENT_WRITE_PRE, (whDmaFlags){0});
4067-
req->state.addr = stateAddr;
4089+
if (ret == WH_ERROR_OK) {
4090+
req->state.addr = stateAddr;
4091+
}
40684092

40694093
if (ret == WH_ERROR_OK) {
40704094
ret = wh_Client_DmaProcessClientAddress(
40714095
ctx, (uintptr_t)in, (void**)&inAddr, req->input.sz,
40724096
WH_DMA_OPER_CLIENT_READ_PRE, (whDmaFlags){0});
4073-
req->input.addr = inAddr;
4097+
if (ret == WH_ERROR_OK) {
4098+
req->input.addr = inAddr;
4099+
}
40744100
}
40754101

40764102
if (ret == WH_ERROR_OK) {
40774103
ret = wh_Client_DmaProcessClientAddress(
40784104
ctx, (uintptr_t)out, (void**)&outAddr, req->output.sz,
40794105
WH_DMA_OPER_CLIENT_WRITE_PRE, (whDmaFlags){0});
4080-
req->output.addr = outAddr;
4106+
if (ret == WH_ERROR_OK) {
4107+
req->output.addr = outAddr;
4108+
}
40814109
}
40824110
}
40834111

40844112
/* Caller invoked SHA Update:
40854113
* wc_CryptoCb_Sha512Hash(sha512, data, len, NULL) */
4086-
if (in != NULL) {
4114+
if (in != NULL && ret == WH_ERROR_OK) {
40874115
req->finalize = 0;
40884116

40894117
#ifdef DEBUG_CRYPTOCB_VERBOSE

0 commit comments

Comments
 (0)