11/*
22 * timing.cpp
33 *
4+ * Copyright The Mbed TLS Contributors
45 * Copyright (C) 2021, Arm Limited, All Rights Reserved
56 * SPDX-License-Identifier: Apache-2.0
67 *
2324#else
2425#include MBEDTLS_CONFIG_FILE
2526#endif
27+
28+ #if defined(MBEDTLS_TIMING_ALT)
29+
2630#include " mbedtls/timing.h"
2731#include " drivers/Timeout.h"
32+ #include " drivers/LowPowerTimeout.h"
33+ #include " drivers/Timer.h"
34+ #include " drivers/LowPowerTimer.h"
2835#include < chrono>
2936
3037extern " C" {
@@ -38,30 +45,101 @@ static void handle_alarm(void)
3845
3946extern " C" void mbedtls_set_alarm (int seconds)
4047{
48+ #if DEVICE_LPTICKER
49+ static mbed::LowPowerTimeout t;
50+ #elif DEVICE_USTICKER
4151 static mbed::Timeout t;
52+ #else
53+ #error "MBEDTLS_TIMING_C requires either LPTICKER or USTICKER"
54+ #endif
55+
4256 mbedtls_timing_alarmed = 0 ;
4357
4458 t.attach (handle_alarm, std::chrono::seconds (seconds));
4559}
4660
61+ // The static Mbed timer here is initialized once only.
62+ // Mbed TLS can have multiple timers (mbedtls_timing_hr_time) derived
63+ // from the Mbed timer.
64+ #if DEVICE_LPTICKER
65+ static mbed::LowPowerTimer timer;
66+ #elif DEVICE_USTICKER
67+ static mbed::Timer timer;
68+ #else
69+ #error "MBEDTLS_TIMING_C requires either LPTICKER or USTICKER"
70+ #endif
71+ static int timer_init = 0 ;
72+
4773#if !defined(HAVE_HARDCLOCK)
4874#define HAVE_HARDCLOCK
49- #include " platform/mbed_rtc_time.h"
50- static int hardclock_init = 0 ;
51- static struct timeval tv_init;
5275
5376extern " C" unsigned long mbedtls_timing_hardclock (void )
5477{
55- struct timeval tv_cur;
56-
57- if (hardclock_init == 0 )
58- {
59- gettimeofday (&tv_init, NULL );
60- hardclock_init = 1 ;
78+ if (timer_init == 0 ) {
79+ timer.reset ();
80+ timer.start ();
81+ timer_init = 1 ;
6182 }
6283
63- gettimeofday (&tv_cur, NULL );
64- return ((tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
65- + (tv_cur.tv_usec - tv_init.tv_usec ));
84+ return timer.elapsed_time ().count ();
6685}
6786#endif /* !HAVE_HARDCLOCK */
87+
88+ extern " C" unsigned long mbedtls_timing_get_timer (struct mbedtls_timing_hr_time *val, int reset)
89+ {
90+ if (timer_init == 0 ) {
91+ timer.reset ();
92+ timer.start ();
93+ timer_init = 1 ;
94+ }
95+
96+ if (reset) {
97+ val->start = std::chrono::duration_cast<std::chrono::milliseconds>(timer.elapsed_time ()).count ();
98+ return 0 ;
99+ } else {
100+ return std::chrono::duration_cast<std::chrono::milliseconds>(timer.elapsed_time ()).count () - val->start ;
101+ }
102+ }
103+
104+ /* *
105+ * Note: The following implementations come from the default timing.c
106+ * from Mbed TLS. They are disabled in timing.c when MBEDTLS_TIMING_ALT
107+ * is defined, but the implementation is nonetheless applicable to
108+ * Mbed OS, so we copy them over.
109+ */
110+
111+ extern " C" void mbedtls_timing_set_delay (void *data, uint32_t int_ms, uint32_t fin_ms)
112+ {
113+ mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
114+
115+ ctx->int_ms = int_ms;
116+ ctx->fin_ms = fin_ms;
117+
118+ if (fin_ms != 0 ) {
119+ (void ) mbedtls_timing_get_timer (&ctx->timer , 1 );
120+ }
121+ }
122+
123+ extern " C" int mbedtls_timing_get_delay (void *data)
124+ {
125+ mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
126+ unsigned long elapsed_ms;
127+
128+ if (ctx->fin_ms == 0 ) {
129+ return -1 ;
130+ }
131+
132+ elapsed_ms = mbedtls_timing_get_timer (&ctx->timer , 0 );
133+
134+ if (elapsed_ms >= ctx->fin_ms ) {
135+ return 2 ;
136+ }
137+
138+ if (elapsed_ms >= ctx->int_ms ) {
139+ return 1 ;
140+ }
141+
142+ return 0 ;
143+ }
144+
145+ #endif // MBEDTLS_TIMING_ALT
0 commit comments