/src/mbedtls/library/timing.c
Line | Count | Source |
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(_WIN32) && !defined(MBEDTLS_PLATFORM_IS_UNIXLIKE) |
17 | | #error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in mbedtls_config.h" |
18 | | #endif |
19 | | |
20 | | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
21 | | |
22 | | #include <windows.h> |
23 | | #include <process.h> |
24 | | |
25 | | struct _hr_time { |
26 | | LARGE_INTEGER start; |
27 | | }; |
28 | | |
29 | | #else |
30 | | |
31 | | #include <unistd.h> |
32 | | #include <sys/types.h> |
33 | | #include <signal.h> |
34 | | /* time.h should be included independently of MBEDTLS_HAVE_TIME. If the |
35 | | * platform matches the ifdefs above, it will be used. */ |
36 | | #include <time.h> |
37 | | #include <sys/time.h> |
38 | | struct _hr_time { |
39 | | struct timeval start; |
40 | | }; |
41 | | #endif /* _WIN32 && !EFIX64 && !EFI32 */ |
42 | | |
43 | | /** |
44 | | * \brief Return the elapsed time in milliseconds |
45 | | * |
46 | | * \warning May change without notice |
47 | | * |
48 | | * \param val points to a timer structure |
49 | | * \param reset If 0, query the elapsed time. Otherwise (re)start the timer. |
50 | | * |
51 | | * \return Elapsed time since the previous reset in ms. When |
52 | | * restarting, this is always 0. |
53 | | * |
54 | | * \note To initialize a timer, call this function with reset=1. |
55 | | * |
56 | | * Determining the elapsed time and resetting the timer is not |
57 | | * atomic on all platforms, so after the sequence |
58 | | * `{ get_timer(1); ...; time1 = get_timer(1); ...; time2 = |
59 | | * get_timer(0) }` the value time1+time2 is only approximately |
60 | | * the delay since the first reset. |
61 | | */ |
62 | | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
63 | | |
64 | | unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset) |
65 | | { |
66 | | struct _hr_time *t = (struct _hr_time *) val; |
67 | | |
68 | | if (reset) { |
69 | | QueryPerformanceCounter(&t->start); |
70 | | return 0; |
71 | | } else { |
72 | | unsigned long delta; |
73 | | LARGE_INTEGER now, hfreq; |
74 | | QueryPerformanceCounter(&now); |
75 | | QueryPerformanceFrequency(&hfreq); |
76 | | delta = (unsigned long) ((now.QuadPart - t->start.QuadPart) * 1000ul |
77 | | / hfreq.QuadPart); |
78 | | return delta; |
79 | | } |
80 | | } |
81 | | |
82 | | #else /* _WIN32 && !EFIX64 && !EFI32 */ |
83 | | |
84 | | unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset) |
85 | 34.3k | { |
86 | 34.3k | struct _hr_time *t = (struct _hr_time *) val; |
87 | | |
88 | 34.3k | if (reset) { |
89 | 27.1k | gettimeofday(&t->start, NULL); |
90 | 27.1k | return 0; |
91 | 27.1k | } else { |
92 | 7.16k | unsigned long delta; |
93 | 7.16k | struct timeval now; |
94 | 7.16k | gettimeofday(&now, NULL); |
95 | 7.16k | delta = (now.tv_sec - t->start.tv_sec) * 1000ul |
96 | 7.16k | + (now.tv_usec - t->start.tv_usec) / 1000; |
97 | 7.16k | return delta; |
98 | 7.16k | } |
99 | 34.3k | } |
100 | | |
101 | | #endif /* _WIN32 && !EFIX64 && !EFI32 */ |
102 | | |
103 | | /* |
104 | | * Set delays to watch |
105 | | */ |
106 | | void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms) |
107 | 35.7k | { |
108 | 35.7k | mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data; |
109 | | |
110 | 35.7k | ctx->int_ms = int_ms; |
111 | 35.7k | ctx->fin_ms = fin_ms; |
112 | | |
113 | 35.7k | if (fin_ms != 0) { |
114 | 27.1k | (void) mbedtls_timing_get_timer(&ctx->timer, 1); |
115 | 27.1k | } |
116 | 35.7k | } |
117 | | |
118 | | /* |
119 | | * Get number of delays expired |
120 | | */ |
121 | | int mbedtls_timing_get_delay(void *data) |
122 | 9.40k | { |
123 | 9.40k | mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data; |
124 | 9.40k | unsigned long elapsed_ms; |
125 | | |
126 | 9.40k | if (ctx->fin_ms == 0) { |
127 | 2.24k | return -1; |
128 | 2.24k | } |
129 | | |
130 | 7.16k | elapsed_ms = mbedtls_timing_get_timer(&ctx->timer, 0); |
131 | | |
132 | 7.16k | if (elapsed_ms >= ctx->fin_ms) { |
133 | 0 | return 2; |
134 | 0 | } |
135 | | |
136 | 7.16k | if (elapsed_ms >= ctx->int_ms) { |
137 | 0 | return 1; |
138 | 0 | } |
139 | | |
140 | 7.16k | return 0; |
141 | 7.16k | } |
142 | | |
143 | | /* |
144 | | * Get the final delay. |
145 | | */ |
146 | | uint32_t mbedtls_timing_get_final_delay( |
147 | | const mbedtls_timing_delay_context *data) |
148 | 0 | { |
149 | 0 | return data->fin_ms; |
150 | 0 | } |
151 | | #endif /* !MBEDTLS_TIMING_ALT */ |
152 | | #endif /* MBEDTLS_TIMING_C */ |