Skip to content

Commit ee7e9ba

Browse files
committed
Resolve conflicts and errors after rebase
Updated based on Copilot’s suggestions
1 parent 7987f9a commit ee7e9ba

File tree

6 files changed

+156
-151
lines changed

6 files changed

+156
-151
lines changed

src/wh_client_crypto.c

Lines changed: 32 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,27 @@ static int _getCryptoResponse(uint8_t* respBuf, uint16_t type,
180180

181181
return header->rc;
182182
}
183+
static int _wait_response_with_crypttimeout(whClientContext *ctx,
184+
uint16_t *out_group, uint16_t *out_action,
185+
uint16_t *out_size, void* data)
186+
{
187+
int ret = WH_ERROR_OK;
188+
do {
189+
ret = wh_Client_RecvResponse(ctx, out_group, out_action, out_size, data);
190+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
191+
if (ret == WH_ERROR_NOTREADY) {
192+
/* Check for crypto timeout */
193+
int chk = wh_CommClient_CheckTimeout(ctx->comm);
194+
if (chk == WH_ERROR_CRYPTIMEOUT) {
195+
return WH_ERROR_CRYPTIMEOUT;
196+
} else if (chk < 0 && chk != WH_ERROR_OK) {
197+
return chk;
198+
}
199+
}
200+
#endif
201+
} while (ret == WH_ERROR_NOTREADY);
202+
return ret;
203+
}
183204

184205
/** Implementations */
185206
int wh_Client_RngGenerate(whClientContext* ctx, uint8_t* out, uint32_t size)
@@ -240,18 +261,8 @@ int wh_Client_RngGenerate(whClientContext* ctx, uint8_t* out, uint32_t size)
240261
#endif
241262
if (ret == 0) {
242263
do {
243-
ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len,
264+
ret = _wait_response_with_crypttimeout(ctx, &group, &action, &res_len,
244265
dataPtr);
245-
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
246-
if (ret == WH_ERROR_NOTREADY) {
247-
/* Check for crypto timeout */
248-
if (wh_CommClient_CheckTimeout(ctx->comm)
249-
== WH_ERROR_CRYPTIMEOUT) {
250-
ret = WH_ERROR_CRYPTIMEOUT;
251-
break;
252-
}
253-
}
254-
#endif
255266
} while (ret == WH_ERROR_NOTREADY);
256267
}
257268
if (ret == WH_ERROR_OK) {
@@ -444,17 +455,8 @@ int wh_Client_AesCtr(whClientContext* ctx, Aes* aes, int enc, const uint8_t* in,
444455
uint16_t res_len = 0;
445456
do {
446457
ret =
447-
wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr);
448-
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
449-
if (ret == WH_ERROR_NOTREADY) {
450-
/* Check for crypto timeout */
451-
if (wh_CommClient_CheckTimeout(ctx->comm)
452-
== WH_ERROR_CRYPTIMEOUT) {
453-
ret = WH_ERROR_CRYPTIMEOUT;
454-
break;
455-
}
456-
}
457-
#endif
458+
_wait_response_with_crypttimeout(ctx, &group, &action,
459+
&res_len, dataPtr);
458460
} while (ret == WH_ERROR_NOTREADY);
459461

460462
if (ret == WH_ERROR_OK) {
@@ -582,17 +584,8 @@ int wh_Client_AesEcb(whClientContext* ctx, Aes* aes, int enc, const uint8_t* in,
582584
uint16_t res_len = 0;
583585
do {
584586
ret =
585-
wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr);
586-
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
587-
if (ret == WH_ERROR_NOTREADY) {
588-
/* Check for crypto timeout */
589-
if (wh_CommClient_CheckTimeout(ctx->comm)
590-
== WH_ERROR_CRYPTIMEOUT) {
591-
ret = WH_ERROR_CRYPTIMEOUT;
592-
break;
593-
}
594-
}
595-
#endif
587+
_wait_response_with_crypttimeout(ctx, &group, &action,
588+
&res_len, dataPtr);
596589
} while (ret == WH_ERROR_NOTREADY);
597590

598591
if (ret == WH_ERROR_OK) {
@@ -717,17 +710,8 @@ int wh_Client_AesCbc(whClientContext* ctx, Aes* aes, int enc, const uint8_t* in,
717710
uint16_t res_len = 0;
718711
do {
719712
ret =
720-
wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr);
721-
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
722-
if (ret == WH_ERROR_NOTREADY) {
723-
/* Check for crypto timeout */
724-
if (wh_CommClient_CheckTimeout(ctx->comm)
725-
== WH_ERROR_CRYPTIMEOUT) {
726-
ret = WH_ERROR_CRYPTIMEOUT;
727-
break;
728-
}
729-
}
730-
#endif
713+
_wait_response_with_crypttimeout(ctx, &group, &action,
714+
&res_len, dataPtr);
731715
} while (ret == WH_ERROR_NOTREADY);
732716

733717
if (ret == WH_ERROR_OK) {
@@ -865,17 +849,8 @@ int wh_Client_AesGcm(whClientContext* ctx, Aes* aes, int enc, const uint8_t* in,
865849
uint16_t res_len = 0;
866850
do {
867851
ret =
868-
wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr);
869-
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
870-
if (ret == WH_ERROR_NOTREADY) {
871-
/* Check for crypto timeout */
872-
if (wh_CommClient_CheckTimeout(ctx->comm)
873-
== WH_ERROR_CRYPTIMEOUT) {
874-
ret = WH_ERROR_CRYPTIMEOUT;
875-
break;
876-
}
877-
}
878-
#endif
852+
_wait_response_with_crypttimeout(ctx, &group, &action,
853+
&res_len, dataPtr);
879854
} while (ret == WH_ERROR_NOTREADY);
880855

881856
if (ret == WH_ERROR_OK) {
@@ -1074,17 +1049,8 @@ int wh_Client_AesGcmDma(whClientContext* ctx, Aes* aes, int enc,
10741049
uint16_t resLen = 0;
10751050
do {
10761051
ret =
1077-
wh_Client_RecvResponse(ctx, &group, &action, &resLen, dataPtr);
1078-
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
1079-
if (ret == WH_ERROR_NOTREADY) {
1080-
/* Check for crypto timeout */
1081-
if (wh_CommClient_CheckTimeout(ctx->comm)
1082-
== WH_ERROR_CRYPTIMEOUT) {
1083-
ret = WH_ERROR_CRYPTIMEOUT;
1084-
break;
1085-
}
1086-
}
1087-
#endif
1052+
_wait_response_with_crypttimeout(ctx, &group, &action,
1053+
&resLen, dataPtr);
10881054
} while (ret == WH_ERROR_NOTREADY);
10891055

10901056
if (ret == WH_ERROR_OK) {

src/wh_comm.c

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ int wh_CommClient_Init(whCommClient* context, const whCommClientConfig* config)
7676
context->connect_cb = config->connect_cb;
7777
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
7878
context->crypt_timeout_cb = config->crypt_timeout_cb;
79-
context->cryptimeout_enabled = config->cryptimeout_enabled;
79+
context->crypt_timeout_enabled = config->crypt_timeout_enabled;
8080
context->crypt_timeout = config->crypt_timeout;
8181
#endif
8282
if (context->transport_cb->Init != NULL) {
@@ -216,50 +216,65 @@ int wh_CommClient_Cleanup(whCommClient* context)
216216
}
217217

218218
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
219+
static uint64_t wh_timeval_to_ms64(const WOLFHSM_TIMEVAL* tv)
220+
{
221+
if (tv == NULL) return 0;
222+
return (uint64_t)tv->tv_sec * 1000ULL + (uint64_t)((tv->tv_usec) / 1000ULL);
223+
}
219224
/* Set Crypto Time Out if needed */
220225
int wh_CommClient_InitCryptTimeout(whCommClient* context)
221226
{
222-
if (context == NULL || context->crypt_timeout_cb == NULL ||
223-
context->crypt_timeout_cb->GetCurrentTime == NULL) {
227+
if (context == NULL)
228+
return WH_ERROR_BADARGS;
229+
/* if feature not enabled, nothing to do */
230+
if (context->crypt_timeout_enabled != 1)
231+
return WH_ERROR_OK;
232+
if (context->crypt_timeout_cb == NULL ||
233+
context->crypt_timeout_cb->GetCurrentTime == NULL)
224234
return WH_ERROR_BADARGS;
225-
}
226235

227-
if (context->cryptimeout_enabled == 1) {
228-
context->crypt_timeout_cb->start_time =
236+
/* cache conversion of crypt_timeout to milliseconds */
237+
context->crypt_timeout_ms = wh_timeval_to_ms64(&context->crypt_timeout);
238+
/* initialize start time */
239+
context->crypt_start_time =
229240
context->crypt_timeout_cb->GetCurrentTime(1);
230-
}
231241

232242
return WH_ERROR_OK;
233243
}
234244

235245
/* Check Crypto Timeout */
236246
int wh_CommClient_CheckTimeout(whCommClient* context)
237247
{
238-
uint32_t current_ms = 0;
239-
uint32_t elapsed_ms = 0;
240-
uint32_t timeout_ms = 0;
248+
uint64_t current_ms = 0;
249+
uint64_t elapsed_ms = 0;
250+
uint64_t timeout_ms = 0;
251+
252+
if (context == NULL) return WH_ERROR_BADARGS;
241253

242-
if (context == NULL || context->crypt_timeout_cb == NULL) {
254+
if (context->crypt_timeout_enabled != 1)
255+
return WH_ERROR_OK;
256+
257+
if (context->crypt_timeout_cb == NULL ||
258+
context->crypt_timeout_cb->GetCurrentTime == NULL)
243259
return WH_ERROR_BADARGS;
244-
}
245-
timeout_ms = (uint32_t)(context->crypt_timeout.tv_sec * 1000 +
246-
context->crypt_timeout.tv_usec / 1000);
247260

248-
if (context->cryptimeout_enabled == 1 && timeout_ms > 0) {
249-
/* check timeout by user cb if defined */
250-
if (context->crypt_timeout_cb->CheckTimeout != NULL) {
261+
timeout_ms = context->crypt_timeout_ms;
262+
if (timeout_ms == 0)
263+
return WH_ERROR_OK;
264+
265+
/* check timeout by user cb if defined */
266+
if (context->crypt_timeout_cb->CheckTimeout != NULL) {
251267
return context->crypt_timeout_cb->CheckTimeout(
252-
&context->crypt_timeout_cb->start_time, timeout_ms);
253-
}
254-
else {
255-
/* expect to return time in milliseconds */
256-
current_ms = context->crypt_timeout_cb->GetCurrentTime(0);
257-
elapsed_ms = current_ms - context->crypt_timeout_cb->start_time;
258-
if (elapsed_ms > timeout_ms) {
259-
return WH_ERROR_CRYPTIMEOUT;
260-
}
261-
}
268+
&context->crypt_start_time, timeout_ms);
262269
}
270+
271+
/* Otherwise compute elapsed using user-provided GetCurrentTime */
272+
current_ms = context->crypt_timeout_cb->GetCurrentTime(0);
273+
elapsed_ms = current_ms - context->crypt_start_time;
274+
if (elapsed_ms > timeout_ms) {
275+
return WH_ERROR_CRYPTIMEOUT;
276+
}
277+
263278
return WH_ERROR_OK;
264279
}
265280
#endif /* WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT */

test/wh_test_common.c

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,32 +94,50 @@ int whTest_NvmCfgBackend(whTestNvmBackendType type,
9494
}
9595

9696
#if defined(WOLFHSM_CFG_TEST_CLIENT_CRYPTIMEOUT)
97-
uint32_t whTest_GetCurrentTime(int reset)
97+
#include <time.h>
98+
#include <sys/time.h> /* For gettimeofday */
99+
100+
uint64_t whTest_GetCurrentTime(int reset)
98101
{
99-
struct timeval tv;
100102
(void)reset;
103+
#if defined(CLOCK_MONOTONIC)
104+
struct timespec ts;
105+
106+
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
107+
return 0;
108+
109+
/* Convert to milliseconds number. */
110+
return (uint64_t)ts.tv_sec * 1000ULL +
111+
(uint64_t)ts.tv_nsec / 1000000ULL;
112+
#else
113+
struct timeval tv;
101114
if (gettimeofday(&tv, 0) < 0)
102115
return 0;
103116
/* Convert to milliseconds number. */
104-
return (uint32_t)(tv.tv_sec * 1000 + tv.tv_usec / 1000);
117+
return (uint64_t)(tv.tv_sec * 1000ULL + tv.tv_usec / 1000ULL);
118+
#endif
105119
}
106120
/* start_time stores the time (in milliseconds) returned by the GetCurrentTime()
107121
* callback when the operation started.
108122
* The actual unit depends on the GetCurrentTime() implementation.
109123
* timeout_ms represents the timeout in milliseconds, which is derived from
110124
* the crypt_timeout value in whCommClientConfig.
111125
*/
112-
int whTest_CheckCryptoTimeout(uint32_t* start_time, uint32_t timeout_ms)
126+
int whTest_CheckCryptoTimeout(uint64_t* start_time, uint64_t timeout_ms)
113127
{
114-
uint32_t current_time = whTest_GetCurrentTime(0);
115-
uint32_t elapsed_time = current_time - *start_time;
128+
uint64_t current_time;
129+
uint64_t elapsed_time;
130+
131+
if (start_time == NULL) return WH_ERROR_BADARGS;
132+
if (timeout_ms == 0) return WH_ERROR_OK;
133+
134+
current_time = whTest_GetCurrentTime(0);
135+
elapsed_time = current_time - *start_time;
116136

117-
if (timeout_ms == 0) {
118-
return WH_ERROR_OK;
119-
}
120137
if (elapsed_time > timeout_ms) {
121138
return WH_ERROR_CRYPTIMEOUT;
122139
}
140+
123141
return WH_ERROR_OK;
124142
}
125143
#endif /* WOLFHSM_CFG_TEST_CLIENT_CRYPTIMEOUT */

test/wh_test_common.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,13 @@ int whTest_NvmCfgBackend(whTestNvmBackendType type,
139139
whTestNvmBackendUnion* nvmSetup, whNvmConfig* nvmCfg,
140140
whFlashRamsimCfg* fCfg, whFlashRamsimCtx* fCtx,
141141
const whFlashCb* fCb);
142-
uint32_t whTest_GetCurrentTime(int reset);
143-
int whTest_CheckCryptoTimeout(uint32_t* start_time, uint32_t timeout_ms);
142+
uint64_t whTest_GetCurrentTime(int reset);
143+
int whTest_CheckCryptoTimeout(uint64_t* start_time, uint64_t timeout_ms);
144144

145145
#define WH_CLIENT_CRYPTO_TIMEOUT_CB \
146146
{ \
147147
.GetCurrentTime = whTest_GetCurrentTime, \
148148
.CheckTimeout = whTest_CheckCryptoTimeout, \
149-
.start_time = 0, \
150149
}
151150

152151
#endif /* WH_TEST_COMMON_H_ */

0 commit comments

Comments
 (0)