blob: 58f1c1ec2eba8765d25bc0dabb4d6ea4de7f5ad1 [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
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7
Gilles Peskinedb09ef62020-06-03 01:43:33 +02008#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00009
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +020010#if defined(MBEDTLS_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000013
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +020014#if !defined(MBEDTLS_TIMING_ALT)
15
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010016#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040017 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
Ørjan Malde479d8de2020-05-20 09:32:39 +000018 !defined(__HAIKU__) && !defined(__midipix__)
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020019#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 +010020#endif
21
Paul Bakkerfa6a6202013-10-28 18:48:30 +010022#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000023
24#include <windows.h>
irwire931d0e2018-06-23 18:55:14 +030025#include <process.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Gilles Peskine449bd832023-01-11 14:50:10 +010027struct _hr_time {
Paul Bakker5121ce52009-01-03 21:22:43 +000028 LARGE_INTEGER start;
29};
30
31#else
32
33#include <unistd.h>
34#include <sys/types.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000035#include <signal.h>
Andrzej Kurek57353692022-04-07 08:08:21 -040036/* time.h should be included independently of MBEDTLS_HAVE_TIME. If the
37 * platform matches the ifdefs above, it will be used. */
Paul Bakker5121ce52009-01-03 21:22:43 +000038#include <time.h>
Andrzej Kurek09e803c2022-03-02 11:20:23 -050039#include <sys/time.h>
Gilles Peskine449bd832023-01-11 14:50:10 +010040struct _hr_time {
Paul Bakker5121ce52009-01-03 21:22:43 +000041 struct timeval start;
42};
Paul Bakker9af723c2014-05-01 13:03:14 +020043#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000044
TRodziewicz963bb812021-06-18 13:22:57 +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 */
Paul Bakkerfa6a6202013-10-28 18:48:30 +010064#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Gilles Peskine449bd832023-01-11 14:50:10 +010066unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
Paul Bakker5121ce52009-01-03 21:22:43 +000067{
Paul Bakker5121ce52009-01-03 21:22:43 +000068 struct _hr_time *t = (struct _hr_time *) val;
69
Gilles Peskine449bd832023-01-11 14:50:10 +010070 if (reset) {
71 QueryPerformanceCounter(&t->start);
72 return 0;
73 } else {
Gilles Peskined92f0aa2017-10-16 19:33:06 +020074 unsigned long delta;
75 LARGE_INTEGER now, hfreq;
Gilles Peskine449bd832023-01-11 14:50:10 +010076 QueryPerformanceCounter(&now);
77 QueryPerformanceFrequency(&hfreq);
78 delta = (unsigned long) ((now.QuadPart - t->start.QuadPart) * 1000ul
79 / hfreq.QuadPart);
80 return delta;
Gilles Peskined92f0aa2017-10-16 19:33:06 +020081 }
Paul Bakker5121ce52009-01-03 21:22:43 +000082}
83
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010084#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000085
Gilles Peskine449bd832023-01-11 14:50:10 +010086unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
Paul Bakker5121ce52009-01-03 21:22:43 +000087{
Paul Bakker5121ce52009-01-03 21:22:43 +000088 struct _hr_time *t = (struct _hr_time *) val;
89
Gilles Peskine449bd832023-01-11 14:50:10 +010090 if (reset) {
91 gettimeofday(&t->start, NULL);
92 return 0;
93 } else {
Gilles Peskined92f0aa2017-10-16 19:33:06 +020094 unsigned long delta;
95 struct timeval now;
Gilles Peskine449bd832023-01-11 14:50:10 +010096 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;
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200100 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000101}
102
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100103#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200105/*
106 * Set delays to watch
107 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100108void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +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
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 if (fin_ms != 0) {
116 (void) mbedtls_timing_get_timer(&ctx->timer, 1);
117 }
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200118}
119
120/*
121 * Get number of delays expired
122 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100123int mbedtls_timing_get_delay(void *data)
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200124{
125 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
126 unsigned long elapsed_ms;
127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 if (ctx->fin_ms == 0) {
129 return -1;
130 }
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 elapsed_ms = mbedtls_timing_get_timer(&ctx->timer, 0);
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 if (elapsed_ms >= ctx->fin_ms) {
135 return 2;
136 }
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 if (elapsed_ms >= ctx->int_ms) {
139 return 1;
140 }
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 return 0;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200143}
Paul Elliottb9af2db2022-03-09 15:34:37 +0000144
145/*
146 * Get the final delay.
147 */
148uint32_t mbedtls_timing_get_final_delay(
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 const mbedtls_timing_delay_context *data)
Paul Elliottb9af2db2022-03-09 15:34:37 +0000150{
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 return data->fin_ms;
Paul Elliottb9af2db2022-03-09 15:34:37 +0000152}
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200153#endif /* !MBEDTLS_TIMING_ALT */
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200154#endif /* MBEDTLS_TIMING_C */