blob: 6852033ea61174a05e712648e252784200fe1947 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Portable interface to the CPU cycle counter
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
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.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +020022#if defined(MBEDTLS_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +020026#if !defined(MBEDTLS_TIMING_ALT)
27
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010028#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040029 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
Ørjan Malde479d8de2020-05-20 09:32:39 +000030 !defined(__HAIKU__) && !defined(__midipix__)
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020031#error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in mbedtls_config.h"
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010032#endif
33
Paul Bakkerfa6a6202013-10-28 18:48:30 +010034#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000035
36#include <windows.h>
irwire931d0e2018-06-23 18:55:14 +030037#include <process.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Gilles Peskine449bd832023-01-11 14:50:10 +010039struct _hr_time {
Paul Bakker5121ce52009-01-03 21:22:43 +000040 LARGE_INTEGER start;
41};
42
43#else
44
45#include <unistd.h>
46#include <sys/types.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000047#include <signal.h>
Andrzej Kurek57353692022-04-07 08:08:21 -040048/* time.h should be included independently of MBEDTLS_HAVE_TIME. If the
49 * platform matches the ifdefs above, it will be used. */
Paul Bakker5121ce52009-01-03 21:22:43 +000050#include <time.h>
Andrzej Kurek09e803c2022-03-02 11:20:23 -050051#include <sys/time.h>
Gilles Peskine449bd832023-01-11 14:50:10 +010052struct _hr_time {
Paul Bakker5121ce52009-01-03 21:22:43 +000053 struct timeval start;
54};
Paul Bakker9af723c2014-05-01 13:03:14 +020055#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000056
TRodziewicz963bb812021-06-18 13:22:57 +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 */
Paul Bakkerfa6a6202013-10-28 18:48:30 +010076#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000077
Gilles Peskine449bd832023-01-11 14:50:10 +010078unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
Paul Bakker5121ce52009-01-03 21:22:43 +000079{
Paul Bakker5121ce52009-01-03 21:22:43 +000080 struct _hr_time *t = (struct _hr_time *) val;
81
Gilles Peskine449bd832023-01-11 14:50:10 +010082 if (reset) {
83 QueryPerformanceCounter(&t->start);
84 return 0;
85 } else {
Gilles Peskined92f0aa2017-10-16 19:33:06 +020086 unsigned long delta;
87 LARGE_INTEGER now, hfreq;
Gilles Peskine449bd832023-01-11 14:50:10 +010088 QueryPerformanceCounter(&now);
89 QueryPerformanceFrequency(&hfreq);
90 delta = (unsigned long) ((now.QuadPart - t->start.QuadPart) * 1000ul
91 / hfreq.QuadPart);
92 return delta;
Gilles Peskined92f0aa2017-10-16 19:33:06 +020093 }
Paul Bakker5121ce52009-01-03 21:22:43 +000094}
95
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010096#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000097
Gilles Peskine449bd832023-01-11 14:50:10 +010098unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
Paul Bakker5121ce52009-01-03 21:22:43 +000099{
Paul Bakker5121ce52009-01-03 21:22:43 +0000100 struct _hr_time *t = (struct _hr_time *) val;
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 if (reset) {
103 gettimeofday(&t->start, NULL);
104 return 0;
105 } else {
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200106 unsigned long delta;
107 struct timeval now;
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 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;
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200112 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000113}
114
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100115#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200117/*
118 * Set delays to watch
119 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +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
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 if (fin_ms != 0) {
128 (void) mbedtls_timing_get_timer(&ctx->timer, 1);
129 }
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200130}
131
132/*
133 * Get number of delays expired
134 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100135int mbedtls_timing_get_delay(void *data)
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200136{
137 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
138 unsigned long elapsed_ms;
139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 if (ctx->fin_ms == 0) {
141 return -1;
142 }
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 elapsed_ms = mbedtls_timing_get_timer(&ctx->timer, 0);
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 if (elapsed_ms >= ctx->fin_ms) {
147 return 2;
148 }
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 if (elapsed_ms >= ctx->int_ms) {
151 return 1;
152 }
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 return 0;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200155}
Paul Elliottb9af2db2022-03-09 15:34:37 +0000156
157/*
158 * Get the final delay.
159 */
160uint32_t mbedtls_timing_get_final_delay(
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 const mbedtls_timing_delay_context *data)
Paul Elliottb9af2db2022-03-09 15:34:37 +0000162{
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 return data->fin_ms;
Paul Elliottb9af2db2022-03-09 15:34:37 +0000164}
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200165#endif /* !MBEDTLS_TIMING_ALT */
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200166#endif /* MBEDTLS_TIMING_C */