Skip to content

Commit 4b1af43

Browse files
committed
Add crypto timeout to RNG and AES
1 parent bbb9297 commit 4b1af43

File tree

10 files changed

+450
-14
lines changed

10 files changed

+450
-14
lines changed

src/wh_client_crypto.c

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,23 @@ int wh_Client_RngGenerate(whClientContext* ctx, uint8_t* out, uint32_t size)
221221

222222
/* Send request and get response */
223223
ret = wh_Client_SendRequest(ctx, group, action, req_len, dataPtr);
224+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
225+
if (ret == WH_ERROR_OK) {
226+
ret = wh_CommClient_InitCryptTimeout(ctx->comm);
227+
}
228+
#endif
224229
if (ret == 0) {
225230
do {
226231
ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len,
227232
dataPtr);
228-
} while (ret == WH_ERROR_NOTREADY);
233+
} while ((ret == WH_ERROR_NOTREADY)
234+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
235+
&&
236+
((ret = wh_CommClient_CheckTimeout(ctx->comm)) !=
237+
WH_ERROR_CRYPTIMEOUT));
238+
#else
239+
);
240+
#endif
229241
}
230242
if (ret == WH_ERROR_OK) {
231243
/* Get response */
@@ -339,14 +351,26 @@ int wh_Client_AesCtr(whClientContext* ctx, Aes* aes, int enc, const uint8_t* in,
339351
wh_Utils_Hexdump("[client] req packet: \n", (uint8_t*)req, req_len);
340352
#endif
341353
ret = wh_Client_SendRequest(ctx, group, action, req_len, dataPtr);
354+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
355+
if (ret == WH_ERROR_OK) {
356+
ret = wh_CommClient_InitCryptTimeout(ctx->comm);
357+
}
358+
#endif
342359
/* read response */
343360
if (ret == WH_ERROR_OK) {
344361
/* Response packet */
345362
uint16_t res_len = 0;
346363
do {
347364
ret =
348365
wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr);
349-
} while (ret == WH_ERROR_NOTREADY);
366+
} while ((ret == WH_ERROR_NOTREADY)
367+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
368+
&&
369+
((ret = wh_CommClient_CheckTimeout(ctx->comm)) !=
370+
WH_ERROR_CRYPTIMEOUT));
371+
#else
372+
);
373+
#endif
350374
if (ret == WH_ERROR_OK) {
351375
ret = _getCryptoResponse(dataPtr, type, (uint8_t**)&res);
352376
if (ret == WH_ERROR_OK) {
@@ -460,14 +484,26 @@ int wh_Client_AesEcb(whClientContext* ctx, Aes* aes, int enc, const uint8_t* in,
460484
wh_Utils_Hexdump("[client] req packet: \n", (uint8_t*)req, req_len);
461485
#endif
462486
ret = wh_Client_SendRequest(ctx, group, action, req_len, dataPtr);
487+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
488+
if (ret == WH_ERROR_OK) {
489+
ret = wh_CommClient_InitCryptTimeout(ctx->comm);
490+
}
491+
#endif
463492
/* read response */
464493
if (ret == WH_ERROR_OK) {
465494
/* Response packet */
466495
uint16_t res_len = 0;
467496
do {
468497
ret =
469498
wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr);
470-
} while (ret == WH_ERROR_NOTREADY);
499+
} while ((ret == WH_ERROR_NOTREADY)
500+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
501+
&&
502+
((ret = wh_CommClient_CheckTimeout(ctx->comm)) !=
503+
WH_ERROR_CRYPTIMEOUT));
504+
#else
505+
);
506+
#endif
471507
if (ret == WH_ERROR_OK) {
472508
ret = _getCryptoResponse(dataPtr, type, (uint8_t**)&res);
473509
if (ret == WH_ERROR_OK) {
@@ -576,14 +612,26 @@ int wh_Client_AesCbc(whClientContext* ctx, Aes* aes, int enc, const uint8_t* in,
576612
wh_Utils_Hexdump("[client] req packet: \n", (uint8_t*)req, req_len);
577613
#endif
578614
ret = wh_Client_SendRequest(ctx, group, action, req_len, dataPtr);
615+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
616+
if (ret == WH_ERROR_OK) {
617+
ret = wh_CommClient_InitCryptTimeout(ctx->comm);
618+
}
619+
#endif
579620
/* read response */
580621
if (ret == WH_ERROR_OK) {
581622
/* Response packet */
582623
uint16_t res_len = 0;
583624
do {
584625
ret =
585626
wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr);
586-
} while (ret == WH_ERROR_NOTREADY);
627+
} while ((ret == WH_ERROR_NOTREADY)
628+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
629+
&&
630+
((ret = wh_CommClient_CheckTimeout(ctx->comm)) !=
631+
WH_ERROR_CRYPTIMEOUT));
632+
#else
633+
);
634+
#endif
587635
if (ret == WH_ERROR_OK) {
588636
ret = _getCryptoResponse(dataPtr, type, (uint8_t**)&res);
589637
if (ret == WH_ERROR_OK) {
@@ -708,12 +756,24 @@ int wh_Client_AesGcm(whClientContext* ctx, Aes* aes, int enc, const uint8_t* in,
708756

709757
/* Send request and receive response */
710758
ret = wh_Client_SendRequest(ctx, group, action, req_len, dataPtr);
759+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
760+
if (ret == WH_ERROR_OK) {
761+
ret = wh_CommClient_InitCryptTimeout(ctx->comm);
762+
}
763+
#endif
711764
if (ret == 0) {
712765
uint16_t res_len = 0;
713766
do {
714767
ret =
715768
wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr);
716-
} while (ret == WH_ERROR_NOTREADY);
769+
} while ((ret == WH_ERROR_NOTREADY)
770+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
771+
&&
772+
((ret = wh_CommClient_CheckTimeout(ctx->comm)) !=
773+
WH_ERROR_CRYPTIMEOUT));
774+
#else
775+
);
776+
#endif
717777

718778
if (ret == WH_ERROR_OK) {
719779
/* Get response */
@@ -902,12 +962,24 @@ int wh_Client_AesGcmDma(whClientContext* ctx, Aes* aes, int enc,
902962
wh_Utils_Hexdump("[client] AESGCM DMA req packet: \n", dataPtr, reqLen);
903963
#endif
904964
ret = wh_Client_SendRequest(ctx, group, action, reqLen, dataPtr);
965+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
966+
if (ret == WH_ERROR_OK) {
967+
ret = wh_CommClient_InitCryptTimeout(ctx->comm);
968+
}
969+
#endif
905970
if (ret == 0) {
906971
uint16_t resLen = 0;
907972
do {
908973
ret =
909974
wh_Client_RecvResponse(ctx, &group, &action, &resLen, dataPtr);
910-
} while (ret == WH_ERROR_NOTREADY);
975+
} while ((ret == WH_ERROR_NOTREADY)
976+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
977+
&&
978+
((ret = wh_CommClient_CheckTimeout(ctx->comm)) !=
979+
WH_ERROR_CRYPTIMEOUT));
980+
#else
981+
);
982+
#endif
911983

912984
if (ret == WH_ERROR_OK) {
913985
/* Get response */

src/wh_comm.c

Lines changed: 52 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->cryptimeout_enabled= config->cryptimeout_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,53 @@ int wh_CommClient_Cleanup(whCommClient* context)
211215
return rc;
212216
}
213217

218+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT)
219+
/* Set Crypto Time Out if needed */
220+
int wh_CommClient_InitCryptTimeout(whCommClient* context)
221+
{
222+
if (context == NULL || context->crypt_timeout_cb == NULL ||
223+
context->crypt_timeout_cb->GetCurrentTime == NULL) {
224+
return WH_ERROR_BADARGS;
225+
}
226+
227+
if (context->cryptimeout_enabled == 1) {
228+
context->crypt_timeout_cb->start_time =
229+
context->crypt_timeout_cb->GetCurrentTime(1);
230+
}
231+
232+
return WH_ERROR_OK;
233+
}
234+
235+
/* Check Crypto Timeout */
236+
int wh_CommClient_CheckTimeout(whCommClient* context)
237+
{
238+
uint32_t current_ms = 0;
239+
uint32_t elapsed_ms = 0;
240+
uint32_t timeout_ms = 0;
241+
242+
if (context == NULL || context->crypt_timeout_cb == NULL) {
243+
return WH_ERROR_BADARGS;
244+
}
245+
timeout_ms = (uint32_t)(context->crypt_timeout.tv_sec * 1000 +
246+
context->crypt_timeout.tv_usec / 1000);
247+
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) {
251+
return context->crypt_timeout_cb->CheckTimeout(
252+
&context->crypt_timeout_cb->start_time, timeout_ms);
253+
} else {
254+
/* expect to return time in milliseconds */
255+
current_ms = context->crypt_timeout_cb->GetCurrentTime(0);
256+
elapsed_ms = current_ms - context->crypt_timeout_cb->start_time;
257+
if (elapsed_ms > timeout_ms) {
258+
return WH_ERROR_CRYPTIMEOUT;
259+
}
260+
}
261+
}
262+
return WH_ERROR_OK;
263+
}
264+
#endif /* WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT */
214265
#endif /* WOLFHSM_CFG_ENABLE_CLIENT */
215266

216267
/** Server Functions */

test/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ else
129129
DEF += -DWOLFHSM_CFG_IS_TEST_SERVER
130130
endif
131131

132+
ifeq ($(CRYPTIMEOUT),1)
133+
DEF += -DWOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT
134+
endif
132135

133136
## Source files
134137
# Assembly source files

test/config/wolfhsm_cfg.h

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

5959
#define WOLFHSM_CFG_SERVER_NVM_FLASH_LOG
6060

61+
/* Enable client crypto timeout feature for testing */
62+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT_CRYPTIMEOUT) && \
63+
defined(WOLFHSM_CFG_TEST_POSIX)
64+
#define WOLFHSM_CFG_CLIENT_CRYPTIMEOUT_SEC (2)
65+
#define WOLFHSM_CFG_TEST_CLIENT_CRYPTIMEOUT
66+
#endif /* WOLFHSM_CFG_TEST_CLIENT_CRYPTIMEOUT */
67+
6168
#endif /* WOLFHSM_CFG_H_ */

test/wh_test_common.c

Lines changed: 34 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,34 @@ int whTest_NvmCfgBackend(whTestNvmBackendType type,
9092

9193
return 0;
9294
}
95+
96+
#if defined(WOLFHSM_CFG_TEST_CLIENT_CRYPTIMEOUT)
97+
uint32_t whTest_GetCurrentTime(int reset)
98+
{
99+
struct timeval tv;
100+
(void)reset;
101+
if (gettimeofday(&tv, 0) < 0)
102+
return 0;
103+
/* Convert to milliseconds number. */
104+
return (uint32_t)(tv.tv_sec * 1000 + tv.tv_usec / 1000);
105+
}
106+
/* start_time stores the time (in milliseconds) returned by the GetCurrentTime()
107+
* callback when the operation started.
108+
* The actual unit depends on the GetCurrentTime() implementation.
109+
* timeout_ms represents the timeout in milliseconds, which is derived from
110+
* the crypt_timeout value in whCommClientConfig.
111+
*/
112+
int whTest_CheckCryptoTimeout(uint32_t* start_time, uint32_t timeout_ms)
113+
{
114+
uint32_t current_time = whTest_GetCurrentTime(0);
115+
uint32_t elapsed_time = current_time - *start_time;
116+
117+
if (timeout_ms == 0) {
118+
return WH_ERROR_OK;
119+
}
120+
if (elapsed_time > timeout_ms) {
121+
return WH_ERROR_CRYPTIMEOUT;
122+
}
123+
return WH_ERROR_OK;
124+
}
125+
#endif /* WOLFHSM_CFG_TEST_CLIENT_CRYPTIMEOUT */

test/wh_test_common.h

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

0 commit comments

Comments
 (0)