-
Notifications
You must be signed in to change notification settings - Fork 25
Implement timeout capability. Apply timeout to crypto response #278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /* | ||
| * Copyright (C) 2025 wolfSSL Inc. | ||
| * | ||
| * This file is part of wolfHSM. | ||
| * | ||
| * wolfHSM is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation; either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * wolfHSM is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with wolfHSM. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| /* | ||
| * src/wh_timeout.c | ||
| */ | ||
|
|
||
| /* Pick up compile-time configuration */ | ||
| #include "wolfhsm/wh_settings.h" | ||
|
|
||
| #include "wolfhsm/wh_timeout.h" | ||
| #include "wolfhsm/wh_error.h" | ||
|
|
||
| int wh_Timeout_Init(whTimeoutCtx* timeout, const whTimeoutConfig* config) | ||
| { | ||
| if ((timeout == NULL) || (config == NULL)) { | ||
| return WH_ERROR_BADARGS; | ||
| } | ||
|
|
||
| timeout->startUs = 0; | ||
| timeout->timeoutUs = config->timeoutUs; | ||
| timeout->expiredCb = config->expiredCb; | ||
| timeout->cbCtx = config->cbCtx; | ||
|
|
||
| return WH_ERROR_OK; | ||
| } | ||
|
|
||
| int wh_Timeout_Set(whTimeoutCtx* timeout, uint64_t timeoutUs) | ||
| { | ||
| if (timeout == NULL) { | ||
| return WH_ERROR_BADARGS; | ||
| } | ||
|
|
||
| timeout->timeoutUs = timeoutUs; | ||
|
|
||
| return WH_ERROR_OK; | ||
| } | ||
|
|
||
| int wh_Timeout_Start(whTimeoutCtx* timeout) | ||
| { | ||
| if (timeout == NULL) { | ||
| return WH_ERROR_BADARGS; | ||
| } | ||
|
|
||
| timeout->startUs = WH_GETTIME_US(); | ||
|
|
||
| return WH_ERROR_OK; | ||
| } | ||
|
|
||
| int wh_Timeout_Stop(whTimeoutCtx* timeout) | ||
| { | ||
| if (timeout == NULL) { | ||
| return WH_ERROR_BADARGS; | ||
| } | ||
|
|
||
| timeout->startUs = 0; | ||
| timeout->timeoutUs = 0; | ||
|
|
||
| return WH_ERROR_OK; | ||
| } | ||
|
|
||
| int wh_Timeout_Expired(const whTimeoutCtx* timeout) | ||
| { | ||
| uint64_t nowUs = 0; | ||
| int expired = 0; | ||
|
|
||
| if (timeout == NULL) { | ||
| return 0; | ||
| } | ||
|
|
||
| if (timeout->timeoutUs == 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| nowUs = WH_GETTIME_US(); | ||
| expired = (nowUs - timeout->startUs) >= timeout->timeoutUs; | ||
| if (expired && (timeout->expiredCb != NULL)) { | ||
| timeout->expiredCb(timeout->cbCtx); | ||
| } | ||
| return expired; | ||
|
Comment on lines
+77
to
+95
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * Copyright (C) 2024 wolfSSL Inc. | ||
| * | ||
| * This file is part of wolfHSM. | ||
| * | ||
| * wolfHSM is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation; either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * wolfHSM is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with wolfHSM. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| /* | ||
| * test/wh_test_timeout.c | ||
| * | ||
| */ | ||
|
|
||
| #include <stdint.h> | ||
|
|
||
| #include "wolfhsm/wh_settings.h" | ||
| #include "wolfhsm/wh_timeout.h" | ||
| #include "wolfhsm/wh_error.h" | ||
|
|
||
| #include "wh_test_common.h" | ||
| #include "wh_test_timeout.h" | ||
|
|
||
| static void whTest_TimeoutCb(void* ctx) | ||
| { | ||
| int* counter = (int*)ctx; | ||
| if (counter != NULL) { | ||
| (*counter)++; | ||
| } | ||
| } | ||
|
|
||
| int whTest_Timeout(void) | ||
| { | ||
| int cb_count = 0; | ||
| whTimeoutConfig cfg; | ||
| whTimeoutCtx timeout[1]; | ||
|
|
||
| cfg.timeoutUs = 1; | ||
| cfg.expiredCb = whTest_TimeoutCb; | ||
| cfg.cbCtx = &cb_count; | ||
|
|
||
| wh_Timeout_Init(timeout, &cfg); | ||
| WH_TEST_ASSERT_RETURN(timeout->startUs == 0); | ||
| WH_TEST_ASSERT_RETURN(timeout->timeoutUs == cfg.timeoutUs); | ||
| WH_TEST_ASSERT_RETURN(timeout->expiredCb == cfg.expiredCb); | ||
| WH_TEST_ASSERT_RETURN(timeout->cbCtx == cfg.cbCtx); | ||
|
|
||
| wh_Timeout_Start(timeout); | ||
| WH_TEST_ASSERT_RETURN(timeout->timeoutUs > 0); | ||
|
|
||
| wh_Timeout_Stop(timeout); | ||
| WH_TEST_ASSERT_RETURN(timeout->startUs == 0); | ||
| WH_TEST_ASSERT_RETURN(timeout->timeoutUs == 0); | ||
|
|
||
| /* No expiration when disabled */ | ||
| WH_TEST_ASSERT_RETURN(wh_Timeout_Expired(timeout) == 0); | ||
|
|
||
| WH_TEST_ASSERT_RETURN(wh_Timeout_Init(0, 0) == WH_ERROR_BADARGS); | ||
| WH_TEST_ASSERT_RETURN(wh_Timeout_Set(0, 0) == WH_ERROR_BADARGS); | ||
| WH_TEST_ASSERT_RETURN(wh_Timeout_Start(0) == WH_ERROR_BADARGS); | ||
| WH_TEST_ASSERT_RETURN(wh_Timeout_Stop(0) == WH_ERROR_BADARGS); | ||
| WH_TEST_ASSERT_RETURN(wh_Timeout_Expired(0) == 0); | ||
|
Comment on lines
+64
to
+71
|
||
|
|
||
| return 0; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * Copyright (C) 2024 wolfSSL Inc. | ||
| * | ||
| * This file is part of wolfHSM. | ||
| * | ||
| * wolfHSM is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation; either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * wolfHSM is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with wolfHSM. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| /* | ||
| * test/wh_test_timeout.h | ||
| * | ||
| */ | ||
|
|
||
| #ifndef TEST_WH_TEST_TIMEOUT_H_ | ||
| #define TEST_WH_TEST_TIMEOUT_H_ | ||
|
|
||
| /** | ||
| * Runs timeout module tests. | ||
| * | ||
| * @return 0 on success and a non-zero error code on failure. | ||
| */ | ||
| int whTest_Timeout(void); | ||
|
|
||
| #endif /* TEST_WH_TEST_TIMEOUT_H_ */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wh_Client_RecvResponseTimeoutassumes thetimeoutcontext is already configured with a non-zerotimeoutUs, but whenWOLFHSM_CFG_ENABLE_TIMEOUTis defined it is legal forwhClientConfig.respTimeoutConfigto be NULL, leavingtimeoutUsat 0. In that case this function will never time out and will behave like the original infinite wait loop, which can be surprising to integrators who enable timeout support but forget to provide a configuration; consider either validating thattimeout->timeoutUsis non-zero here (and failing fast) or documenting/enforcing that a validrespTimeoutConfigmust be supplied when timeout support is enabled.