Skip to content

Commit 7cae6bc

Browse files
committed
Add crypto timeout to RNG and AES
Updated based on Copilot's suggestions
1 parent 398de7f commit 7cae6bc

File tree

10 files changed

+492
-30
lines changed

10 files changed

+492
-30
lines changed

src/wh_client_crypto.c

Lines changed: 65 additions & 6 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)
@@ -233,9 +254,14 @@ int wh_Client_RngGenerate(whClientContext* ctx, uint8_t* out, uint32_t size)
233254

234255
/* Send request and get response */
235256
ret = wh_Client_SendRequest(ctx, group, action, req_len, dataPtr);
257+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
258+
if (ret == WH_ERROR_OK) {
259+
ret = wh_CommClient_InitCryptTimeout(ctx->comm);
260+
}
261+
#endif
236262
if (ret == 0) {
237263
do {
238-
ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len,
264+
ret = _wait_response_with_crypttimeout(ctx, &group, &action, &res_len,
239265
dataPtr);
240266
} while (ret == WH_ERROR_NOTREADY);
241267
}
@@ -418,14 +444,21 @@ int wh_Client_AesCtr(whClientContext* ctx, Aes* aes, int enc, const uint8_t* in,
418444
wh_Utils_Hexdump("[client] req packet: \n", (uint8_t*)req, req_len);
419445
#endif
420446
ret = wh_Client_SendRequest(ctx, group, action, req_len, dataPtr);
447+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
448+
if (ret == WH_ERROR_OK) {
449+
ret = wh_CommClient_InitCryptTimeout(ctx->comm);
450+
}
451+
#endif
421452
/* read response */
422453
if (ret == WH_ERROR_OK) {
423454
/* Response packet */
424455
uint16_t res_len = 0;
425456
do {
426457
ret =
427-
wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr);
458+
_wait_response_with_crypttimeout(ctx, &group, &action,
459+
&res_len, dataPtr);
428460
} while (ret == WH_ERROR_NOTREADY);
461+
429462
if (ret == WH_ERROR_OK) {
430463
ret = _getCryptoResponse(dataPtr, type, (uint8_t**)&res);
431464
if (ret == WH_ERROR_OK) {
@@ -540,14 +573,21 @@ int wh_Client_AesEcb(whClientContext* ctx, Aes* aes, int enc, const uint8_t* in,
540573
wh_Utils_Hexdump("[client] req packet: \n", (uint8_t*)req, req_len);
541574
#endif
542575
ret = wh_Client_SendRequest(ctx, group, action, req_len, dataPtr);
576+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
577+
if (ret == WH_ERROR_OK) {
578+
ret = wh_CommClient_InitCryptTimeout(ctx->comm);
579+
}
580+
#endif
543581
/* read response */
544582
if (ret == WH_ERROR_OK) {
545583
/* Response packet */
546584
uint16_t res_len = 0;
547585
do {
548586
ret =
549-
wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr);
587+
_wait_response_with_crypttimeout(ctx, &group, &action,
588+
&res_len, dataPtr);
550589
} while (ret == WH_ERROR_NOTREADY);
590+
551591
if (ret == WH_ERROR_OK) {
552592
ret = _getCryptoResponse(dataPtr, type, (uint8_t**)&res);
553593
if (ret == WH_ERROR_OK) {
@@ -659,14 +699,21 @@ int wh_Client_AesCbc(whClientContext* ctx, Aes* aes, int enc, const uint8_t* in,
659699
wh_Utils_Hexdump("[client] req packet: \n", (uint8_t*)req, req_len);
660700
#endif
661701
ret = wh_Client_SendRequest(ctx, group, action, req_len, dataPtr);
702+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
703+
if (ret == WH_ERROR_OK) {
704+
ret = wh_CommClient_InitCryptTimeout(ctx->comm);
705+
}
706+
#endif
662707
/* read response */
663708
if (ret == WH_ERROR_OK) {
664709
/* Response packet */
665710
uint16_t res_len = 0;
666711
do {
667712
ret =
668-
wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr);
713+
_wait_response_with_crypttimeout(ctx, &group, &action,
714+
&res_len, dataPtr);
669715
} while (ret == WH_ERROR_NOTREADY);
716+
670717
if (ret == WH_ERROR_OK) {
671718
ret = _getCryptoResponse(dataPtr, type, (uint8_t**)&res);
672719
if (ret == WH_ERROR_OK) {
@@ -793,11 +840,17 @@ int wh_Client_AesGcm(whClientContext* ctx, Aes* aes, int enc, const uint8_t* in,
793840

794841
/* Send request and receive response */
795842
ret = wh_Client_SendRequest(ctx, group, action, req_len, dataPtr);
843+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
844+
if (ret == WH_ERROR_OK) {
845+
ret = wh_CommClient_InitCryptTimeout(ctx->comm);
846+
}
847+
#endif
796848
if (ret == 0) {
797849
uint16_t res_len = 0;
798850
do {
799851
ret =
800-
wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr);
852+
_wait_response_with_crypttimeout(ctx, &group, &action,
853+
&res_len, dataPtr);
801854
} while (ret == WH_ERROR_NOTREADY);
802855

803856
if (ret == WH_ERROR_OK) {
@@ -987,11 +1040,17 @@ int wh_Client_AesGcmDma(whClientContext* ctx, Aes* aes, int enc,
9871040
wh_Utils_Hexdump("[client] AESGCM DMA req packet: \n", dataPtr, reqLen);
9881041
#endif
9891042
ret = wh_Client_SendRequest(ctx, group, action, reqLen, dataPtr);
1043+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
1044+
if (ret == WH_ERROR_OK) {
1045+
ret = wh_CommClient_InitCryptTimeout(ctx->comm);
1046+
}
1047+
#endif
9901048
if (ret == 0) {
9911049
uint16_t resLen = 0;
9921050
do {
9931051
ret =
994-
wh_Client_RecvResponse(ctx, &group, &action, &resLen, dataPtr);
1052+
_wait_response_with_crypttimeout(ctx, &group, &action,
1053+
&resLen, dataPtr);
9951054
} while (ret == WH_ERROR_NOTREADY);
9961055

9971056
if (ret == WH_ERROR_OK) {

src/wh_comm.c

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ int wh_CommClient_Init(whCommClient* context, const whCommClientConfig* config)
7474
context->transport_context = config->transport_context;
7575
context->client_id = config->client_id;
7676
context->connect_cb = config->connect_cb;
77-
77+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
78+
context->crypt_timeout_cb = config->crypt_timeout_cb;
79+
context->crypt_timeout_enabled = config->crypt_timeout_enabled;
80+
context->crypt_timeout = config->crypt_timeout;
81+
#endif
7882
if (context->transport_cb->Init != NULL) {
7983
rc = context->transport_cb->Init(context->transport_context,
8084
config->transport_config, NULL, NULL);
@@ -211,6 +215,69 @@ int wh_CommClient_Cleanup(whCommClient* context)
211215
return rc;
212216
}
213217

218+
#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+
}
224+
/* Set Crypto Time Out if needed */
225+
int wh_CommClient_InitCryptTimeout(whCommClient* context)
226+
{
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)
234+
return WH_ERROR_BADARGS;
235+
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 =
240+
context->crypt_timeout_cb->GetCurrentTime(1);
241+
242+
return WH_ERROR_OK;
243+
}
244+
245+
/* Check Crypto Timeout */
246+
int wh_CommClient_CheckTimeout(whCommClient* context)
247+
{
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;
253+
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)
259+
return WH_ERROR_BADARGS;
260+
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) {
267+
return context->crypt_timeout_cb->CheckTimeout(
268+
&context->crypt_start_time, timeout_ms);
269+
}
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+
278+
return WH_ERROR_OK;
279+
}
280+
#endif /* WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT */
214281
#endif /* WOLFHSM_CFG_ENABLE_CLIENT */
215282

216283
/** Server Functions */

test/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ else
135135
DEF += -DWOLFHSM_CFG_IS_TEST_SERVER
136136
endif
137137

138+
ifeq ($(CRYPTIMEOUT),1)
139+
DEF += -DWOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT
140+
endif
138141

139142
## Source files
140143
# Assembly source files

test/config/wolfhsm_cfg.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,11 @@
6161

6262
#define WOLFHSM_CFG_SERVER_NVM_FLASH_LOG
6363

64+
/* Enable client crypto timeout feature for testing */
65+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT) && \
66+
defined(WOLFHSM_CFG_TEST_POSIX)
67+
#define WOLFHSM_CFG_CLIENT_CRYPTIMEOUT_SEC (2)
68+
#define WOLFHSM_CFG_TEST_CLIENT_CRYPTIMEOUT
69+
#endif /* WOLFHSM_CFG_TEST_CLIENT_CRYPTIMEOUT */
70+
6471
#endif /* WOLFHSM_CFG_H_ */

test/wh_test_common.c

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
#include <wolfhsm/wh_error.h>
2727

2828
#include "wh_test_common.h"
29-
29+
#if defined(WOLFHSM_CFG_TEST_CLIENT_CRYPTIMEOUT)
30+
#include <sys/time.h> /* For gettimeofday */
31+
#endif
3032

3133
/**
3234
* Helper function to configure and select an NVM backend for testing.
@@ -90,3 +92,52 @@ int whTest_NvmCfgBackend(whTestNvmBackendType type,
9092

9193
return 0;
9294
}
95+
96+
#if defined(WOLFHSM_CFG_TEST_CLIENT_CRYPTIMEOUT)
97+
#include <time.h>
98+
#include <sys/time.h> /* For gettimeofday */
99+
100+
uint64_t whTest_GetCurrentTime(int reset)
101+
{
102+
(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;
114+
if (gettimeofday(&tv, 0) < 0)
115+
return 0;
116+
/* Convert to milliseconds number. */
117+
return (uint64_t)(tv.tv_sec * 1000ULL + tv.tv_usec / 1000ULL);
118+
#endif
119+
}
120+
/* start_time stores the time (in milliseconds) returned by the GetCurrentTime()
121+
* callback when the operation started.
122+
* The actual unit depends on the GetCurrentTime() implementation.
123+
* timeout_ms represents the timeout in milliseconds, which is derived from
124+
* the crypt_timeout value in whCommClientConfig.
125+
*/
126+
int whTest_CheckCryptoTimeout(uint64_t* start_time, uint64_t timeout_ms)
127+
{
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;
136+
137+
if (elapsed_time > timeout_ms) {
138+
return WH_ERROR_CRYPTIMEOUT;
139+
}
140+
141+
return WH_ERROR_OK;
142+
}
143+
#endif /* WOLFHSM_CFG_TEST_CLIENT_CRYPTIMEOUT */

test/wh_test_common.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,13 @@ int whTest_NvmCfgBackend(whTestNvmBackendType type,
139139
whTestNvmBackendUnion* nvmSetup, whNvmConfig* nvmCfg,
140140
whFlashRamsimCfg* fCfg, whFlashRamsimCtx* fCtx,
141141
const whFlashCb* fCb);
142+
uint64_t whTest_GetCurrentTime(int reset);
143+
int whTest_CheckCryptoTimeout(uint64_t* start_time, uint64_t timeout_ms);
144+
145+
#define WH_CLIENT_CRYPTO_TIMEOUT_CB \
146+
{ \
147+
.GetCurrentTime = whTest_GetCurrentTime, \
148+
.CheckTimeout = whTest_CheckCryptoTimeout, \
149+
}
150+
142151
#endif /* WH_TEST_COMMON_H_ */

0 commit comments

Comments
 (0)