blob: 58f1c1ec2eba8765d25bc0dabb4d6ea4de7f5ad1 [file] [log] [blame]
Jens Wiklander817466c2018-05-22 13:49:31 +02001/*
2 * Portable interface to the CPU cycle counter
3 *
Jerome Forissier79013242021-07-28 10:24:04 +02004 * Copyright The Mbed TLS Contributors
Tom Van Eyckc1633172024-04-09 18:44:13 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Jens Wiklander817466c2018-05-22 13:49:31 +02006 */
7
Jerome Forissier79013242021-07-28 10:24:04 +02008#include "common.h"
Jens Wiklander817466c2018-05-22 13:49:31 +02009
Jens Wiklander817466c2018-05-22 13:49:31 +020010#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) && \
Jens Wiklander3d3b0592019-03-20 15:30:29 +010017 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
Jerome Forissier79013242021-07-28 10:24:04 +020018 !defined(__HAIKU__) && !defined(__midipix__)
Jens Wiklander32b31802023-10-06 16:59:46 +020019#error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in mbedtls_config.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020020#endif
21
22#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
23
24#include <windows.h>
Jens Wiklander3d3b0592019-03-20 15:30:29 +010025#include <process.h>
Jens Wiklander817466c2018-05-22 13:49:31 +020026
Jens Wiklander32b31802023-10-06 16:59:46 +020027struct _hr_time {
Jens Wiklander817466c2018-05-22 13:49:31 +020028 LARGE_INTEGER start;
29};
30
31#else
32
33#include <unistd.h>
34#include <sys/types.h>
Jens Wiklander817466c2018-05-22 13:49:31 +020035#include <signal.h>
Jerome Forissier039e02d2022-08-09 17:10:15 +020036/* time.h should be included independently of MBEDTLS_HAVE_TIME. If the
37 * platform matches the ifdefs above, it will be used. */
Jens Wiklander817466c2018-05-22 13:49:31 +020038#include <time.h>
Jerome Forissier039e02d2022-08-09 17:10:15 +020039#include <sys/time.h>
Jens Wiklander32b31802023-10-06 16:59:46 +020040struct _hr_time {
Jens Wiklander817466c2018-05-22 13:49:31 +020041 struct timeval start;
42};
Jens Wiklander817466c2018-05-22 13:49:31 +020043#endif /* _WIN32 && !EFIX64 && !EFI32 */
44
Jens Wiklander32b31802023-10-06 16:59:46 +020045/**
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 */
Jens Wiklander817466c2018-05-22 13:49:31 +020064#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
65
Jens Wiklander32b31802023-10-06 16:59:46 +020066unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
Jens Wiklander817466c2018-05-22 13:49:31 +020067{
Jens Wiklander817466c2018-05-22 13:49:31 +020068 struct _hr_time *t = (struct _hr_time *) val;
69
Jens Wiklander32b31802023-10-06 16:59:46 +020070 if (reset) {
71 QueryPerformanceCounter(&t->start);
72 return 0;
73 } else {
Jens Wiklander3d3b0592019-03-20 15:30:29 +010074 unsigned long delta;
75 LARGE_INTEGER now, hfreq;
Jens Wiklander32b31802023-10-06 16:59:46 +020076 QueryPerformanceCounter(&now);
77 QueryPerformanceFrequency(&hfreq);
78 delta = (unsigned long) ((now.QuadPart - t->start.QuadPart) * 1000ul
79 / hfreq.QuadPart);
80 return delta;
Jens Wiklander3d3b0592019-03-20 15:30:29 +010081 }
Jens Wiklander817466c2018-05-22 13:49:31 +020082}
83
Jens Wiklander817466c2018-05-22 13:49:31 +020084#else /* _WIN32 && !EFIX64 && !EFI32 */
85
Jens Wiklander32b31802023-10-06 16:59:46 +020086unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
Jens Wiklander817466c2018-05-22 13:49:31 +020087{
Jens Wiklander817466c2018-05-22 13:49:31 +020088 struct _hr_time *t = (struct _hr_time *) val;
89
Jens Wiklander32b31802023-10-06 16:59:46 +020090 if (reset) {
91 gettimeofday(&t->start, NULL);
92 return 0;
93 } else {
Jens Wiklander3d3b0592019-03-20 15:30:29 +010094 unsigned long delta;
95 struct timeval now;
Jens Wiklander32b31802023-10-06 16:59:46 +020096 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;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100100 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200101}
102
103#endif /* _WIN32 && !EFIX64 && !EFI32 */
104
105/*
106 * Set delays to watch
107 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200108void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
Jens Wiklander817466c2018-05-22 13:49:31 +0200109{
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
Jens Wiklander32b31802023-10-06 16:59:46 +0200115 if (fin_ms != 0) {
116 (void) mbedtls_timing_get_timer(&ctx->timer, 1);
117 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200118}
119
120/*
121 * Get number of delays expired
122 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200123int mbedtls_timing_get_delay(void *data)
Jens Wiklander817466c2018-05-22 13:49:31 +0200124{
125 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
126 unsigned long elapsed_ms;
127
Jens Wiklander32b31802023-10-06 16:59:46 +0200128 if (ctx->fin_ms == 0) {
129 return -1;
130 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200131
Jens Wiklander32b31802023-10-06 16:59:46 +0200132 elapsed_ms = mbedtls_timing_get_timer(&ctx->timer, 0);
Jens Wiklander817466c2018-05-22 13:49:31 +0200133
Jens Wiklander32b31802023-10-06 16:59:46 +0200134 if (elapsed_ms >= ctx->fin_ms) {
135 return 2;
136 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200137
Jens Wiklander32b31802023-10-06 16:59:46 +0200138 if (elapsed_ms >= ctx->int_ms) {
139 return 1;
140 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200141
Jens Wiklander32b31802023-10-06 16:59:46 +0200142 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200143}
144
Jens Wiklander817466c2018-05-22 13:49:31 +0200145/*
Jens Wiklander32b31802023-10-06 16:59:46 +0200146 * Get the final delay.
Jens Wiklander817466c2018-05-22 13:49:31 +0200147 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200148uint32_t mbedtls_timing_get_final_delay(
149 const mbedtls_timing_delay_context *data)
Jens Wiklander817466c2018-05-22 13:49:31 +0200150{
Jens Wiklander32b31802023-10-06 16:59:46 +0200151 return data->fin_ms;
Jens Wiklander817466c2018-05-22 13:49:31 +0200152}
Jerome Forissier039e02d2022-08-09 17:10:15 +0200153#endif /* !MBEDTLS_TIMING_ALT */
Jens Wiklander817466c2018-05-22 13:49:31 +0200154#endif /* MBEDTLS_TIMING_C */