1
1
/*
2
2
* timing.cpp
3
3
*
4
+ * Copyright The Mbed TLS Contributors
4
5
* Copyright (C) 2021, Arm Limited, All Rights Reserved
5
6
* SPDX-License-Identifier: Apache-2.0
6
7
*
23
24
#else
24
25
#include MBEDTLS_CONFIG_FILE
25
26
#endif
27
+
28
+ #if defined(MBEDTLS_TIMING_ALT)
29
+
26
30
#include " mbedtls/timing.h"
27
31
#include " drivers/Timeout.h"
32
+ #include " drivers/LowPowerTimeout.h"
33
+ #include " drivers/Timer.h"
34
+ #include " drivers/LowPowerTimer.h"
28
35
#include < chrono>
29
36
30
37
extern " C" {
@@ -38,30 +45,101 @@ static void handle_alarm(void)
38
45
39
46
extern " C" void mbedtls_set_alarm (int seconds)
40
47
{
48
+ #if DEVICE_LPTICKER
49
+ static mbed::LowPowerTimeout t;
50
+ #elif DEVICE_USTICKER
41
51
static mbed::Timeout t;
52
+ #else
53
+ #error "MBEDTLS_TIMING_C requires either LPTICKER or USTICKER"
54
+ #endif
55
+
42
56
mbedtls_timing_alarmed = 0 ;
43
57
44
58
t.attach (handle_alarm, std::chrono::seconds (seconds));
45
59
}
46
60
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
+
47
73
#if !defined(HAVE_HARDCLOCK)
48
74
#define HAVE_HARDCLOCK
49
- #include " platform/mbed_rtc_time.h"
50
- static int hardclock_init = 0 ;
51
- static struct timeval tv_init;
52
75
53
76
extern " C" unsigned long mbedtls_timing_hardclock (void )
54
77
{
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 ;
61
82
}
62
83
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 ();
66
85
}
67
86
#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