blob: 6852033ea61174a05e712648e252784200fe1947 [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
5 * SPDX-License-Identifier: Apache-2.0
Jens Wiklander817466c2018-05-22 13:49:31 +02006 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Jens Wiklander817466c2018-05-22 13:49:31 +020018 */
19
Jerome Forissier79013242021-07-28 10:24:04 +020020#include "common.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020021
Jens Wiklander817466c2018-05-22 13:49:31 +020022#if defined(MBEDTLS_TIMING_C)
23
24#include "mbedtls/timing.h"
25
26#if !defined(MBEDTLS_TIMING_ALT)
27
28#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Jens Wiklander3d3b0592019-03-20 15:30:29 +010029 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
Jerome Forissier79013242021-07-28 10:24:04 +020030 !defined(__HAIKU__) && !defined(__midipix__)
Jens Wiklander32b31802023-10-06 16:59:46 +020031#error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in mbedtls_config.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020032#endif
33
34#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
35
36#include <windows.h>
Jens Wiklander3d3b0592019-03-20 15:30:29 +010037#include <process.h>
Jens Wiklander817466c2018-05-22 13:49:31 +020038
Jens Wiklander32b31802023-10-06 16:59:46 +020039struct _hr_time {
Jens Wiklander817466c2018-05-22 13:49:31 +020040 LARGE_INTEGER start;
41};
42
43#else
44
45#include <unistd.h>
46#include <sys/types.h>
Jens Wiklander817466c2018-05-22 13:49:31 +020047#include <signal.h>
Jerome Forissier039e02d2022-08-09 17:10:15 +020048/* time.h should be included independently of MBEDTLS_HAVE_TIME. If the
49 * platform matches the ifdefs above, it will be used. */
Jens Wiklander817466c2018-05-22 13:49:31 +020050#include <time.h>
Jerome Forissier039e02d2022-08-09 17:10:15 +020051#include <sys/time.h>
Jens Wiklander32b31802023-10-06 16:59:46 +020052struct _hr_time {
Jens Wiklander817466c2018-05-22 13:49:31 +020053 struct timeval start;
54};
Jens Wiklander817466c2018-05-22 13:49:31 +020055#endif /* _WIN32 && !EFIX64 && !EFI32 */
56
Jens Wiklander32b31802023-10-06 16:59:46 +020057/**
58 * \brief Return the elapsed time in milliseconds
59 *
60 * \warning May change without notice
61 *
62 * \param val points to a timer structure
63 * \param reset If 0, query the elapsed time. Otherwise (re)start the timer.
64 *
65 * \return Elapsed time since the previous reset in ms. When
66 * restarting, this is always 0.
67 *
68 * \note To initialize a timer, call this function with reset=1.
69 *
70 * Determining the elapsed time and resetting the timer is not
71 * atomic on all platforms, so after the sequence
72 * `{ get_timer(1); ...; time1 = get_timer(1); ...; time2 =
73 * get_timer(0) }` the value time1+time2 is only approximately
74 * the delay since the first reset.
75 */
Jens Wiklander817466c2018-05-22 13:49:31 +020076#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
77
Jens Wiklander32b31802023-10-06 16:59:46 +020078unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
Jens Wiklander817466c2018-05-22 13:49:31 +020079{
Jens Wiklander817466c2018-05-22 13:49:31 +020080 struct _hr_time *t = (struct _hr_time *) val;
81
Jens Wiklander32b31802023-10-06 16:59:46 +020082 if (reset) {
83 QueryPerformanceCounter(&t->start);
84 return 0;
85 } else {
Jens Wiklander3d3b0592019-03-20 15:30:29 +010086 unsigned long delta;
87 LARGE_INTEGER now, hfreq;
Jens Wiklander32b31802023-10-06 16:59:46 +020088 QueryPerformanceCounter(&now);
89 QueryPerformanceFrequency(&hfreq);
90 delta = (unsigned long) ((now.QuadPart - t->start.QuadPart) * 1000ul
91 / hfreq.QuadPart);
92 return delta;
Jens Wiklander3d3b0592019-03-20 15:30:29 +010093 }
Jens Wiklander817466c2018-05-22 13:49:31 +020094}
95
Jens Wiklander817466c2018-05-22 13:49:31 +020096#else /* _WIN32 && !EFIX64 && !EFI32 */
97
Jens Wiklander32b31802023-10-06 16:59:46 +020098unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
Jens Wiklander817466c2018-05-22 13:49:31 +020099{
Jens Wiklander817466c2018-05-22 13:49:31 +0200100 struct _hr_time *t = (struct _hr_time *) val;
101
Jens Wiklander32b31802023-10-06 16:59:46 +0200102 if (reset) {
103 gettimeofday(&t->start, NULL);
104 return 0;
105 } else {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100106 unsigned long delta;
107 struct timeval now;
Jens Wiklander32b31802023-10-06 16:59:46 +0200108 gettimeofday(&now, NULL);
109 delta = (now.tv_sec - t->start.tv_sec) * 1000ul
110 + (now.tv_usec - t->start.tv_usec) / 1000;
111 return delta;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100112 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200113}
114
115#endif /* _WIN32 && !EFIX64 && !EFI32 */
116
117/*
118 * Set delays to watch
119 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200120void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
Jens Wiklander817466c2018-05-22 13:49:31 +0200121{
122 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
123
124 ctx->int_ms = int_ms;
125 ctx->fin_ms = fin_ms;
126
Jens Wiklander32b31802023-10-06 16:59:46 +0200127 if (fin_ms != 0) {
128 (void) mbedtls_timing_get_timer(&ctx->timer, 1);
129 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200130}
131
132/*
133 * Get number of delays expired
134 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200135int mbedtls_timing_get_delay(void *data)
Jens Wiklander817466c2018-05-22 13:49:31 +0200136{
137 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
138 unsigned long elapsed_ms;
139
Jens Wiklander32b31802023-10-06 16:59:46 +0200140 if (ctx->fin_ms == 0) {
141 return -1;
142 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200143
Jens Wiklander32b31802023-10-06 16:59:46 +0200144 elapsed_ms = mbedtls_timing_get_timer(&ctx->timer, 0);
Jens Wiklander817466c2018-05-22 13:49:31 +0200145
Jens Wiklander32b31802023-10-06 16:59:46 +0200146 if (elapsed_ms >= ctx->fin_ms) {
147 return 2;
148 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200149
Jens Wiklander32b31802023-10-06 16:59:46 +0200150 if (elapsed_ms >= ctx->int_ms) {
151 return 1;
152 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200153
Jens Wiklander32b31802023-10-06 16:59:46 +0200154 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200155}
156
Jens Wiklander817466c2018-05-22 13:49:31 +0200157/*
Jens Wiklander32b31802023-10-06 16:59:46 +0200158 * Get the final delay.
Jens Wiklander817466c2018-05-22 13:49:31 +0200159 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200160uint32_t mbedtls_timing_get_final_delay(
161 const mbedtls_timing_delay_context *data)
Jens Wiklander817466c2018-05-22 13:49:31 +0200162{
Jens Wiklander32b31802023-10-06 16:59:46 +0200163 return data->fin_ms;
Jens Wiklander817466c2018-05-22 13:49:31 +0200164}
Jerome Forissier039e02d2022-08-09 17:10:15 +0200165#endif /* !MBEDTLS_TIMING_ALT */
Jens Wiklander817466c2018-05-22 13:49:31 +0200166#endif /* MBEDTLS_TIMING_C */