Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ Mapped so far:
get_ciphersuite_id
- ssl_session: init, free; save and load return
`MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE`
- timing: get_timer, set_delay, get_delay, get_final_delay, on a monotonic
clock. `MBEDTLS_TIMING_C` needs Windows or a Unix-like system.

Known differences from Mbed TLS:

Expand Down Expand Up @@ -230,7 +232,8 @@ make MBEDTLS_COMPAT=1 mbedtls-compat-test

runs these against `libwolfpsa.a`. Each is also a target of its own.

- `mbedtls-compat-unit`: `test/mbedtls_compat/x509_crt_test.c`,
- `mbedtls-compat-unit`: `test/mbedtls_compat/timing_test.c` checks the
timers. `x509_crt_test.c`,
`pk_test.c` and `pem_test.c` parse Mbed TLS's test certificates and keys.
`ssl_test.c` checks return codes and verify results, client and server in
one process.
Expand Down
130 changes: 130 additions & 0 deletions src/mbedtls/timing.c
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;
Comment thread
padelsbach marked this conversation as resolved.
Resolved
}
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 */
7 changes: 4 additions & 3 deletions test/mbedtls_compat/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ endif

# Mbed TLS's test data
DATA := $(MBEDTLS_PATH)/framework/data_files
# Certificate, key and PEM parsing, and TLS return codes and verify results
# with a client and a server in one process
UNIT := $(addprefix $(BUILD_DIR)/,x509_crt_test pk_test pem_test ssl_test)
# Parsing, TLS return codes and verify results in one process, and timers
UNIT := $(addprefix $(BUILD_DIR)/,x509_crt_test pk_test pem_test ssl_test \
timing_test)

# The peers are the wolfSSL example server and client, from a wolfSSL
# configured out of tree from WOLFSSL_PATH, so they do not depend on wolfPSA's
Expand All @@ -49,6 +49,7 @@ PROGRAMS := $(addprefix $(BUILD_DIR)/programs/ssl/,mini_client ssl_server \
ifeq ($(MBEDTLS_THREADING_C),1)
PROGRAMS += $(BUILD_DIR)/programs/ssl/ssl_pthread_server
endif
PROGRAMS += $(BUILD_DIR)/programs/x509/load_roots
# Mbed TLS test certificates, data only. certs.c and the generated
# test_certs.h build against the Mbed TLS checkout's own headers.
CERTS := $(BUILD_DIR)/certs.o
Expand Down
12 changes: 9 additions & 3 deletions test/mbedtls_compat/programs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ TLS13="-p 11111 -d -v 4"
TLS12="-p 11111 -d -v 3"
GOT="tls_get() returned"
P=programs/ssl
X=programs/x509
ROOTS="$DATA/test-ca.crt $DATA/test-ca2.crt $DATA/test-ca-sha256.crt"
# mini_client's built-in CA, test-ca2 from 2013, encodes its name as only the
# older server5 certs do, and wolfSSL compares encoded names
SERVER5="-p 4433 -d -c $DATA/server5-expired.crt -k $DATA/server5.key"
Expand All @@ -66,7 +68,8 @@ while IFS='|' read -r name prog args sopts wrap expect want; do
SKIPS=$((SKIPS + 1))
continue
fi
start_server "$sopts"
srv=
[ -z "$sopts" ] || start_server "$sopts"
out=$(cd "$WOLFSSL_PATH" && $wrap $WRAP "$BUILD_DIR/$prog" $args 2>&1)
rc=$?
if [ $rc -eq "$expect" ] && ! echo "$out" | grep -q "not defined" &&
Expand All @@ -76,8 +79,10 @@ while IFS='|' read -r name prog args sopts wrap expect want; do
echo "FAIL: $name: exit $rc: $out"
FAILS=$((FAILS + 1))
fi
kill $srv 2>/dev/null
wait $srv 2>/dev/null
if [ -n "$srv" ]; then
kill $srv 2>/dev/null
wait $srv 2>/dev/null
fi
done <<CASES
TLS 1.3|$C|$LO|$TLS13||0|$GOT 0
TLS 1.2|$C|$LO|$TLS12||0|$GOT 0
Expand All @@ -92,6 +97,7 @@ not a certificate, X509_INVALID_FORMAT|$C|-A README.md $LO|$TLS13||1|$GOT -8576
IP address host|$C|-S $LO $LO|$TLS13||0|$GOT 0
wrong IP address host, VERIFY_FAILED|$C|-S 127.0.0.2 $LO|$TLS13||1|$GOT -9984
IPv6 address host, VERIFY_FAILED|$C|-S ::1 $LO|$TLS13||1|$GOT -9984
load_roots|$X/load_roots|iterations=10 $ROOTS|||0|10 iterations ->
mini_client|$P/mini_client||$SERVER5|$CA2_DATE|0|
mini_client, expired CA, ssl_handshake_failed|$P/mini_client||$SERVER5||7|
CASES
Expand Down
143 changes: 143 additions & 0 deletions test/mbedtls_compat/timing_test.c
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;
}
5 changes: 5 additions & 0 deletions tools/psa_want_probe.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,8 @@ __WPSA_EMIT__MBEDTLS_SSL_PROTO_TLS1_3
#endif
#endif
#endif
/* A monotonic clock, on Windows and Unix-like systems */
#if defined(WOLFPSA_MBEDTLS_COMPAT) && (defined(USE_WINDOWS_API) || \
defined(__unix__) || defined(__APPLE__))
__WPSA_EMIT__MBEDTLS_TIMING_C
#endif /* WOLFPSA_MBEDTLS_COMPAT && a monotonic clock */
1 change: 1 addition & 0 deletions wolfpsa.map
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ WOLFPSA_1.0 {
mbedtls_net_*;
mbedtls_pem_*;
mbedtls_ssl_*;
mbedtls_timing_*;
mbedtls_x509_*;
/* The wolfCrypt objects a devId selects live inside this library,
* so a shared-library user has to register the device against this
Expand Down
Loading
Loading