Skip to content

Commit 269cab5

Browse files
add return value checks, free in benchmark buffer on error, add macro guards
1 parent 61839d4 commit 269cab5

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
@@ -66,6 +66,7 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
6666
#if defined(WOLFHSM_CFG_DMA)
6767
if (devId == WH_DEV_ID_DMA) {
6868
inLen = WOLFHSM_CFG_BENCH_DMA_BUFFER_SIZE;
69+
#if defined(WOLFHSM_CFG_TEST_POSIX)
6970
if (ctx->transportType == WH_BENCH_TRANSPORT_POSIX_DMA) {
7071
/* if static memory was used with DMA then use XMALLOC */
7172
void* heap =
@@ -79,12 +80,14 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
7980
DYNAMIC_TYPE_TMP_BUFFER);
8081
if (out == NULL) {
8182
WH_BENCH_PRINTF("Failed to allocate memory for DMA\n");
83+
XFREE(in, heap, DYNAMIC_TYPE_TMP_BUFFER);
8284
return WH_ERROR_NOSPACE;
8385
}
8486
}
8587
else {
8688
in = WH_BENCH_DMA_BUFFER;
8789
}
90+
#endif /* WOLFHSM_CFG_TEST_POSIX */
8891
}
8992
else
9093
#endif
@@ -156,6 +159,7 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
156159
}
157160
}
158161
#if defined(WOLFHSM_CFG_DMA)
162+
#if defined(WOLFHSM_CFG_TEST_POSIX)
159163
if (devId == WH_DEV_ID_DMA &&
160164
ctx->transportType == WH_BENCH_TRANSPORT_POSIX_DMA) {
161165
/* if static memory was used with DMA then use XFREE */
@@ -164,6 +168,7 @@ int _benchCmacAes(whClientContext* client, whBenchOpContext* ctx, int id,
164168
XFREE(in, heap, DYNAMIC_TYPE_TMP_BUFFER);
165169
XFREE(out, heap, DYNAMIC_TYPE_TMP_BUFFER);
166170
}
171+
#endif /* WOLFHSM_CFG_TEST_POSIX */
167172
#endif
168173
(void)wc_CmacFree(cmac);
169174
return ret;

benchmark/bench_modules/wh_bench_mod_sha2.c

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

src/wh_client_crypto.c

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2992,7 +2992,9 @@ int wh_Client_CmacDma(whClientContext* ctx, Cmac* cmac, CmacType type,
29922992
ret = wh_Client_DmaProcessClientAddress(
29932993
ctx, (uintptr_t)cmac, (void**)&stateAddr, req->state.sz,
29942994
WH_DMA_OPER_CLIENT_READ_PRE, (whDmaFlags){0});
2995-
req->state.addr = stateAddr;
2995+
if (ret == WH_ERROR_OK) {
2996+
req->state.addr = stateAddr;
2997+
}
29962998

29972999
/* Handle different CMAC operations based on input parameters */
29983000
if (ret == WH_ERROR_OK && key != NULL) {
@@ -3001,7 +3003,9 @@ int wh_Client_CmacDma(whClientContext* ctx, Cmac* cmac, CmacType type,
30013003
ret = wh_Client_DmaProcessClientAddress(
30023004
ctx, (uintptr_t)key, (void**)&keyAddr, req->key.sz,
30033005
WH_DMA_OPER_CLIENT_READ_PRE, (whDmaFlags){0});
3004-
req->key.addr = keyAddr;
3006+
if (ret == WH_ERROR_OK) {
3007+
req->key.addr = keyAddr;
3008+
}
30053009
}
30063010

30073011
if (ret == WH_ERROR_OK && in != NULL) {
@@ -3278,20 +3282,26 @@ int wh_Client_Sha256Dma(whClientContext* ctx, wc_Sha256* sha, const uint8_t* in,
32783282
ret = wh_Client_DmaProcessClientAddress(
32793283
ctx, (uintptr_t)sha256, (void**)&stateAddr, req->state.sz,
32803284
WH_DMA_OPER_CLIENT_WRITE_PRE, (whDmaFlags){0});
3281-
req->state.addr = stateAddr;
3285+
if (ret == WH_ERROR_OK) {
3286+
req->state.addr = stateAddr;
3287+
}
32823288

32833289
if (ret == WH_ERROR_OK) {
32843290
ret = wh_Client_DmaProcessClientAddress(
32853291
ctx, (uintptr_t)in, (void**)&inAddr, req->input.sz,
32863292
WH_DMA_OPER_CLIENT_READ_PRE, (whDmaFlags){0});
3287-
req->input.addr = inAddr;
3293+
if (ret == WH_ERROR_OK) {
3294+
req->input.addr = inAddr;
3295+
}
32883296
}
32893297

32903298
if (ret == WH_ERROR_OK) {
32913299
ret = wh_Client_DmaProcessClientAddress(
32923300
ctx, (uintptr_t)out, (void**)&outAddr, req->output.sz,
32933301
WH_DMA_OPER_CLIENT_WRITE_PRE, (whDmaFlags){0});
3294-
req->output.addr = outAddr;
3302+
if (ret == WH_ERROR_OK) {
3303+
req->output.addr = outAddr;
3304+
}
32953305
}
32963306
}
32973307

@@ -3567,26 +3577,32 @@ int wh_Client_Sha224Dma(whClientContext* ctx, wc_Sha224* sha, const uint8_t* in,
35673577
ret = wh_Client_DmaProcessClientAddress(
35683578
ctx, (uintptr_t)sha224, (void**)&stateAddr, req->state.sz,
35693579
WH_DMA_OPER_CLIENT_WRITE_PRE, (whDmaFlags){0});
3570-
req->state.addr = stateAddr;
3580+
if (ret == WH_ERROR_OK) {
3581+
req->state.addr = stateAddr;
3582+
}
35713583

35723584
if (ret == WH_ERROR_OK) {
35733585
ret = wh_Client_DmaProcessClientAddress(
35743586
ctx, (uintptr_t)in, (void**)&inAddr, req->input.sz,
35753587
WH_DMA_OPER_CLIENT_READ_PRE, (whDmaFlags){0});
3576-
req->input.addr = inAddr;
3588+
if (ret == WH_ERROR_OK) {
3589+
req->input.addr = inAddr;
3590+
}
35773591
}
35783592

35793593
if (ret == WH_ERROR_OK) {
35803594
ret = wh_Client_DmaProcessClientAddress(
35813595
ctx, (uintptr_t)out, (void**)&outAddr, req->output.sz,
35823596
WH_DMA_OPER_CLIENT_WRITE_PRE, (whDmaFlags){0});
3583-
req->output.addr = outAddr;
3597+
if (ret == WH_ERROR_OK) {
3598+
req->output.addr = outAddr;
3599+
}
35843600
}
35853601
}
35863602

35873603
/* Caller invoked SHA Update:
35883604
* wc_CryptoCb_Sha224Hash(sha224, data, len, NULL) */
3589-
if (in != NULL) {
3605+
if (in != NULL && ret == WH_ERROR_OK) {
35903606
req->finalize = 0;
35913607

35923608
#ifdef DEBUG_CRYPTOCB_VERBOSE
@@ -3853,26 +3869,32 @@ int wh_Client_Sha384Dma(whClientContext* ctx, wc_Sha384* sha, const uint8_t* in,
38533869
ret = wh_Client_DmaProcessClientAddress(
38543870
ctx, (uintptr_t)sha384, (void**)&stateAddr, req->state.sz,
38553871
WH_DMA_OPER_CLIENT_WRITE_PRE, (whDmaFlags){0});
3856-
req->state.addr = stateAddr;
3872+
if (ret == WH_ERROR_OK) {
3873+
req->state.addr = stateAddr;
3874+
}
38573875

38583876
if (ret == WH_ERROR_OK) {
38593877
ret = wh_Client_DmaProcessClientAddress(
38603878
ctx, (uintptr_t)in, (void**)&inAddr, req->input.sz,
38613879
WH_DMA_OPER_CLIENT_READ_PRE, (whDmaFlags){0});
3862-
req->input.addr = inAddr;
3880+
if (ret == WH_ERROR_OK) {
3881+
req->input.addr = inAddr;
3882+
}
38633883
}
38643884

38653885
if (ret == WH_ERROR_OK) {
38663886
ret = wh_Client_DmaProcessClientAddress(
38673887
ctx, (uintptr_t)out, (void**)&outAddr, req->output.sz,
38683888
WH_DMA_OPER_CLIENT_WRITE_PRE, (whDmaFlags){0});
3869-
req->output.addr = outAddr;
3889+
if (ret == WH_ERROR_OK) {
3890+
req->output.addr = outAddr;
3891+
}
38703892
}
38713893
}
38723894

38733895
/* Caller invoked SHA Update:
38743896
* wc_CryptoCb_Sha384Hash(sha384, data, len, NULL) */
3875-
if (in != NULL) {
3897+
if (in != NULL && ret == WH_ERROR_OK) {
38763898
req->finalize = 0;
38773899
#ifdef DEBUG_CRYPTOCB_VERBOSE
38783900
printf("[client] SHA384 DMA UPDATE: inAddr=%p, inSz=%u\n", in,
@@ -3946,7 +3968,7 @@ int wh_Client_Sha384Dma(whClientContext* ctx, wc_Sha384* sha, const uint8_t* in,
39463968
}
39473969
return ret;
39483970
}
3949-
#endif /* WOLHSM_CFG_DMA */
3971+
#endif /* WOLFHSM_CFG_DMA */
39503972
#endif /* WOLFSSL_SHA384 */
39513973

39523974

@@ -4150,26 +4172,32 @@ int wh_Client_Sha512Dma(whClientContext* ctx, wc_Sha512* sha, const uint8_t* in,
41504172
ret = wh_Client_DmaProcessClientAddress(
41514173
ctx, (uintptr_t)sha512, (void**)&stateAddr, req->state.sz,
41524174
WH_DMA_OPER_CLIENT_WRITE_PRE, (whDmaFlags){0});
4153-
req->state.addr = stateAddr;
4175+
if (ret == WH_ERROR_OK) {
4176+
req->state.addr = stateAddr;
4177+
}
41544178

41554179
if (ret == WH_ERROR_OK) {
41564180
ret = wh_Client_DmaProcessClientAddress(
41574181
ctx, (uintptr_t)in, (void**)&inAddr, req->input.sz,
41584182
WH_DMA_OPER_CLIENT_READ_PRE, (whDmaFlags){0});
4159-
req->input.addr = inAddr;
4183+
if (ret == WH_ERROR_OK) {
4184+
req->input.addr = inAddr;
4185+
}
41604186
}
41614187

41624188
if (ret == WH_ERROR_OK) {
41634189
ret = wh_Client_DmaProcessClientAddress(
41644190
ctx, (uintptr_t)out, (void**)&outAddr, req->output.sz,
41654191
WH_DMA_OPER_CLIENT_WRITE_PRE, (whDmaFlags){0});
4166-
req->output.addr = outAddr;
4192+
if (ret == WH_ERROR_OK) {
4193+
req->output.addr = outAddr;
4194+
}
41674195
}
41684196
}
41694197

41704198
/* Caller invoked SHA Update:
41714199
* wc_CryptoCb_Sha512Hash(sha512, data, len, NULL) */
4172-
if (in != NULL) {
4200+
if (in != NULL && ret == WH_ERROR_OK) {
41734201
req->finalize = 0;
41744202

41754203
#ifdef DEBUG_CRYPTOCB_VERBOSE

0 commit comments

Comments
 (0)