v / thirdparty / mbedtls / library / timing.c
152 lines · 125 sloc · 3.76 KB · 3d9911f887ecec942f9ae2a5be02d064f233b729
Raw
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
25struct _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>
38struct _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
64unsigned 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
84unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
85{
86 struct _hr_time *t = (struct _hr_time *) val;
87
88 if (reset) {
89 gettimeofday(&t->start, NULL);
90 return 0;
91 } else {
92 unsigned long delta;
93 struct timeval now;
94 gettimeofday(&now, NULL);
95 delta = (now.tv_sec - t->start.tv_sec) * 1000ul
96 + (now.tv_usec - t->start.tv_usec) / 1000;
97 return delta;
98 }
99}
100
101#endif /* _WIN32 && !EFIX64 && !EFI32 */
102
103/*
104 * Set delays to watch
105 */
106void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
107{
108 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
109
110 ctx->int_ms = int_ms;
111 ctx->fin_ms = fin_ms;
112
113 if (fin_ms != 0) {
114 (void) mbedtls_timing_get_timer(&ctx->timer, 1);
115 }
116}
117
118/*
119 * Get number of delays expired
120 */
121int mbedtls_timing_get_delay(void *data)
122{
123 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
124 unsigned long elapsed_ms;
125
126 if (ctx->fin_ms == 0) {
127 return -1;
128 }
129
130 elapsed_ms = mbedtls_timing_get_timer(&ctx->timer, 0);
131
132 if (elapsed_ms >= ctx->fin_ms) {
133 return 2;
134 }
135
136 if (elapsed_ms >= ctx->int_ms) {
137 return 1;
138 }
139
140 return 0;
141}
142
143/*
144 * Get the final delay.
145 */
146uint32_t mbedtls_timing_get_final_delay(
147 const mbedtls_timing_delay_context *data)
148{
149 return data->fin_ms;
150}
151#endif /* !MBEDTLS_TIMING_ALT */
152#endif /* MBEDTLS_TIMING_C */
153