Repository navigation
Add timer functions #60
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
Open
padelsbach
wants to merge
1
commit into
wolfSSL:master
Choose a base branch
from
padelsbach:timing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| /* timing.c | ||
| * | ||
| * Copyright (C) 2026 wolfSSL Inc. | ||
| * | ||
| * This file is part of wolfPSA. | ||
| * | ||
| * wolfPSA 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. | ||
| * | ||
| * wolfPSA 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 this program; if not, write to the Free Software | ||
| * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA | ||
| */ | ||
|
|
||
| /* Mbed TLS 4.x timers on a monotonic clock (MBEDTLS_COMPAT=1). */ | ||
|
|
||
| /* clock_gettime() with -std=c99 */ | ||
| #if defined(WOLFPSA_MBEDTLS_COMPAT) && !defined(_POSIX_C_SOURCE) | ||
| #define _POSIX_C_SOURCE 200112L | ||
| #endif /* WOLFPSA_MBEDTLS_COMPAT && !_POSIX_C_SOURCE */ | ||
|
|
||
| #ifdef HAVE_CONFIG_H | ||
| #include <config.h> | ||
| #endif /* HAVE_CONFIG_H */ | ||
|
|
||
| #include "psa_config.h" | ||
|
|
||
| #if defined(WOLFSSL_PSA_ENGINE) && defined(WOLFPSA_MBEDTLS_COMPAT) | ||
|
|
||
| #include <wolfssl/wolfcrypt/logging.h> | ||
| #include <mbedtls/timing.h> | ||
|
|
||
| /* The generated config has it on Windows and Unix-like systems */ | ||
| #if defined(MBEDTLS_TIMING_C) != \ | ||
| (defined(USE_WINDOWS_API) || defined(__unix__) || defined(__APPLE__)) | ||
| #error "MBEDTLS_TIMING_C does not match the platform" | ||
| #endif /* MBEDTLS_TIMING_C != (USE_WINDOWS_API || __unix__ || __APPLE__) */ | ||
|
|
||
| #if defined(MBEDTLS_TIMING_C) | ||
|
|
||
| #ifdef USE_WINDOWS_API | ||
| #include <windows.h> | ||
| #else | ||
| #include <time.h> | ||
| #endif /* USE_WINDOWS_API */ | ||
|
|
||
| /* Milliseconds from a monotonic clock, else from time() as in Mbed TLS */ | ||
| static int64_t MbedTimingNow(void) | ||
| { | ||
| #ifdef USE_WINDOWS_API | ||
| return (int64_t)GetTickCount64(); | ||
| #else | ||
| struct timespec ts; | ||
|
|
||
| if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { | ||
| return (int64_t)time(NULL) * 1000; | ||
| } | ||
| return (int64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000; | ||
| #endif /* USE_WINDOWS_API */ | ||
| } | ||
|
|
||
| unsigned long long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time* val, | ||
| int reset) | ||
| { | ||
| int64_t now; | ||
|
|
||
| WOLFSSL_ENTER("mbedtls_timing_get_timer"); | ||
|
|
||
| if (val == NULL) { | ||
| return 0; | ||
| } | ||
| now = MbedTimingNow(); | ||
| if (reset) { | ||
| val->ms = now; | ||
| return 0; | ||
| } | ||
| return (unsigned long long)(now - val->ms); | ||
| } | ||
|
|
||
| void mbedtls_timing_set_delay(void* data, uint32_t int_ms, uint32_t fin_ms) | ||
| { | ||
| mbedtls_timing_delay_context* ctx = (mbedtls_timing_delay_context*)data; | ||
|
|
||
| WOLFSSL_ENTER("mbedtls_timing_set_delay"); | ||
|
|
||
| if (ctx != NULL) { | ||
| ctx->int_ms = int_ms; | ||
| ctx->fin_ms = fin_ms; | ||
| if (fin_ms != 0) { | ||
| (void)mbedtls_timing_get_timer(&ctx->timer, 1); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /* Returns -1 when cancelled, else how many of the two delays have passed */ | ||
| int mbedtls_timing_get_delay(void* data) | ||
| { | ||
| mbedtls_timing_delay_context* ctx = (mbedtls_timing_delay_context*)data; | ||
| unsigned long long elapsed; | ||
|
|
||
| WOLFSSL_ENTER("mbedtls_timing_get_delay"); | ||
|
|
||
| if (ctx == NULL || ctx->fin_ms == 0) { | ||
| return -1; | ||
| } | ||
| elapsed = mbedtls_timing_get_timer(&ctx->timer, 0); | ||
| if (elapsed >= ctx->fin_ms) { | ||
| return 2; | ||
| } | ||
| return (elapsed >= ctx->int_ms) ? 1 : 0; | ||
| } | ||
|
|
||
| uint32_t mbedtls_timing_get_final_delay( | ||
| const mbedtls_timing_delay_context* data) | ||
| { | ||
| WOLFSSL_ENTER("mbedtls_timing_get_final_delay"); | ||
|
|
||
| return (data == NULL) ? 0 : data->fin_ms; | ||
| } | ||
|
|
||
| #endif /* MBEDTLS_TIMING_C */ | ||
|
|
||
| #endif /* WOLFSSL_PSA_ENGINE && WOLFPSA_MBEDTLS_COMPAT */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| /* timing_test.c | ||
| * | ||
| * Copyright (C) 2026 wolfSSL Inc. | ||
| * | ||
| * This file is part of wolfPSA. | ||
| * | ||
| * wolfPSA 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. | ||
| * | ||
| * wolfPSA 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 this program; if not, write to the Free Software | ||
| * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA | ||
| */ | ||
|
|
||
| /* Mbed TLS 4.x timers. The first two cases are from test_suite_timing. */ | ||
|
|
||
| #include <stdio.h> | ||
| #include <string.h> | ||
| #include <time.h> | ||
|
|
||
| #include <mbedtls/timing.h> | ||
|
|
||
| #define INT_MS 100 | ||
| #define FIN_MS 200 | ||
| /* Upper bound for a wait, in case a delay never passes */ | ||
| #define MAX_WAIT_MS 5000 | ||
|
|
||
| /* Reset writes the time, which is not 0 */ | ||
| static int GetTimer(void) | ||
| { | ||
| struct mbedtls_timing_hr_time t; | ||
| static const struct mbedtls_timing_hr_time zero; | ||
|
|
||
| memset(&t, 0, sizeof(t)); | ||
| return mbedtls_timing_get_timer(&t, 1) == 0 && | ||
| memcmp(&t, &zero, sizeof(t)) != 0 && | ||
| mbedtls_timing_get_timer(&t, 0) < MAX_WAIT_MS; | ||
| } | ||
|
|
||
| static int DelayZero(void) | ||
| { | ||
| mbedtls_timing_delay_context ctx; | ||
|
|
||
| mbedtls_timing_set_delay(&ctx, 0, 0); | ||
| return mbedtls_timing_get_delay(&ctx) == -1 && | ||
| mbedtls_timing_get_final_delay(&ctx) == 0; | ||
| } | ||
|
|
||
| /* In ms: time() moves on once or twice in 1100 ms */ | ||
| static int FollowsTime(void) | ||
| { | ||
| struct mbedtls_timing_hr_time t; | ||
| unsigned long long ms; | ||
| time_t start = time(NULL); | ||
| time_t now; | ||
|
|
||
| (void)mbedtls_timing_get_timer(&t, 1); | ||
| do { | ||
| ms = mbedtls_timing_get_timer(&t, 0); | ||
| now = time(NULL); | ||
| } while (ms < 1100 && now - start < 3); | ||
| return ms >= 1100 && now - start >= 1 && now - start <= 2; | ||
| } | ||
|
|
||
| /* 0, then 1 after INT_MS, then 2 after FIN_MS, by t started just before */ | ||
| static int Delays(void) | ||
| { | ||
| mbedtls_timing_delay_context ctx; | ||
| struct mbedtls_timing_hr_time t; | ||
| unsigned long long elapsed; | ||
| int last; | ||
| int now; | ||
| int ok; | ||
|
|
||
| (void)mbedtls_timing_get_timer(&t, 1); | ||
| mbedtls_timing_set_delay(&ctx, INT_MS, FIN_MS); | ||
| last = mbedtls_timing_get_delay(&ctx); | ||
| ok = last == 0 && mbedtls_timing_get_final_delay(&ctx) == FIN_MS; | ||
| while (ok && last != 2) { | ||
| now = mbedtls_timing_get_delay(&ctx); | ||
| elapsed = mbedtls_timing_get_timer(&t, 0); | ||
| ok = now >= last && elapsed < MAX_WAIT_MS && | ||
| (now < 1 || elapsed >= INT_MS) && (now < 2 || elapsed >= FIN_MS); | ||
| last = now; | ||
| } | ||
| return ok; | ||
| } | ||
|
|
||
| /* A delay of 0 cancels the one running */ | ||
| static int Cancel(void) | ||
| { | ||
| mbedtls_timing_delay_context ctx; | ||
|
|
||
| mbedtls_timing_set_delay(&ctx, INT_MS, FIN_MS); | ||
| mbedtls_timing_set_delay(&ctx, INT_MS, 0); | ||
| return mbedtls_timing_get_delay(&ctx) == -1 && | ||
| mbedtls_timing_get_final_delay(&ctx) == 0; | ||
| } | ||
|
|
||
| static int NullArgs(void) | ||
| { | ||
| mbedtls_timing_set_delay(NULL, INT_MS, FIN_MS); | ||
| return mbedtls_timing_get_timer(NULL, 1) == 0 && | ||
| mbedtls_timing_get_timer(NULL, 0) == 0 && | ||
| mbedtls_timing_get_delay(NULL) == -1 && | ||
| mbedtls_timing_get_final_delay(NULL) == 0; | ||
| } | ||
|
|
||
| static const struct { | ||
| const char* name; | ||
| int (*test)(void); | ||
| } cases[] = { | ||
| { "get timer", GetTimer }, | ||
| { "delay 0ms", DelayZero }, | ||
| { "milliseconds", FollowsTime }, | ||
| { "delays", Delays }, | ||
| { "cancel", Cancel }, | ||
| { "NULL arguments", NullArgs } | ||
| }; | ||
|
|
||
| int main(void) | ||
| { | ||
| size_t i; | ||
| int fails = 0; | ||
|
|
||
| for (i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { | ||
| if (cases[i].test()) { | ||
| printf("PASS: %s\n", cases[i].name); | ||
| } | ||
| else { | ||
| printf("FAIL: %s\n", cases[i].name); | ||
| fails++; | ||
| } | ||
| } | ||
| return (fails == 0) ? 0 : 1; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.