1 /*
2  *  Portable interface to the CPU cycle counter
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6  */
7 
8 #include "common.h"
9 
10 #if defined(MBEDTLS_TIMING_C)
11 
12 #include "mbedtls/timing.h"
13 
14 #if !defined(MBEDTLS_TIMING_ALT)
15 
16 #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
17     !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
18     !defined(__HAIKU__) && !defined(__midipix__)
19 #error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in mbedtls_config.h"
20 #endif
21 
22 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
23 
24 #include <windows.h>
25 #include <process.h>
26 
27 struct _hr_time {
28     LARGE_INTEGER start;
29 };
30 
31 #else
32 
33 #include <unistd.h>
34 #include <sys/types.h>
35 #include <signal.h>
36 /* time.h should be included independently of MBEDTLS_HAVE_TIME. If the
37  * platform matches the ifdefs above, it will be used. */
38 #include <time.h>
39 #include <sys/time.h>
40 struct _hr_time {
41     struct timeval start;
42 };
43 #endif /* _WIN32 && !EFIX64 && !EFI32 */
44 
45 /**
46  * \brief          Return the elapsed time in milliseconds
47  *
48  * \warning        May change without notice
49  *
50  * \param val      points to a timer structure
51  * \param reset    If 0, query the elapsed time. Otherwise (re)start the timer.
52  *
53  * \return         Elapsed time since the previous reset in ms. When
54  *                 restarting, this is always 0.
55  *
56  * \note           To initialize a timer, call this function with reset=1.
57  *
58  *                 Determining the elapsed time and resetting the timer is not
59  *                 atomic on all platforms, so after the sequence
60  *                 `{ get_timer(1); ...; time1 = get_timer(1); ...; time2 =
61  *                 get_timer(0) }` the value time1+time2 is only approximately
62  *                 the delay since the first reset.
63  */
64 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
65 
mbedtls_timing_get_timer(struct mbedtls_timing_hr_time * val,int reset)66 unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
67 {
68     struct _hr_time *t = (struct _hr_time *) val;
69 
70     if (reset) {
71         QueryPerformanceCounter(&t->start);
72         return 0;
73     } else {
74         unsigned long delta;
75         LARGE_INTEGER now, hfreq;
76         QueryPerformanceCounter(&now);
77         QueryPerformanceFrequency(&hfreq);
78         delta = (unsigned long) ((now.QuadPart - t->start.QuadPart) * 1000ul
79                                  / hfreq.QuadPart);
80         return delta;
81     }
82 }
83 
84 #else /* _WIN32 && !EFIX64 && !EFI32 */
85 
mbedtls_timing_get_timer(struct mbedtls_timing_hr_time * val,int reset)86 unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
87 {
88     struct _hr_time *t = (struct _hr_time *) val;
89 
90     if (reset) {
91         gettimeofday(&t->start, NULL);
92         return 0;
93     } else {
94         unsigned long delta;
95         struct timeval now;
96         gettimeofday(&now, NULL);
97         delta = (now.tv_sec  - t->start.tv_sec) * 1000ul
98                 + (now.tv_usec - t->start.tv_usec) / 1000;
99         return delta;
100     }
101 }
102 
103 #endif /* _WIN32 && !EFIX64 && !EFI32 */
104 
105 /*
106  * Set delays to watch
107  */
mbedtls_timing_set_delay(void * data,uint32_t int_ms,uint32_t fin_ms)108 void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
109 {
110     mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
111 
112     ctx->int_ms = int_ms;
113     ctx->fin_ms = fin_ms;
114 
115     if (fin_ms != 0) {
116         (void) mbedtls_timing_get_timer(&ctx->timer, 1);
117     }
118 }
119 
120 /*
121  * Get number of delays expired
122  */
mbedtls_timing_get_delay(void * data)123 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 /*
146  * Get the final delay.
147  */
mbedtls_timing_get_final_delay(const mbedtls_timing_delay_context * data)148 uint32_t mbedtls_timing_get_final_delay(
149     const mbedtls_timing_delay_context *data)
150 {
151     return data->fin_ms;
152 }
153 #endif /* !MBEDTLS_TIMING_ALT */
154 #endif /* MBEDTLS_TIMING_C */
155