Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Portable interface to the CPU cycle counter |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 8 | #include "common.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9 | |
Manuel Pégourié-Gonnard | 8903fe0 | 2015-05-12 19:30:45 +0200 | [diff] [blame] | 10 | #if defined(MBEDTLS_TIMING_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 12 | #include "mbedtls/timing.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 13 | |
Manuel Pégourié-Gonnard | 8903fe0 | 2015-05-12 19:30:45 +0200 | [diff] [blame] | 14 | #if !defined(MBEDTLS_TIMING_ALT) |
| 15 | |
Manuel Pégourié-Gonnard | 325ce09 | 2016-02-22 10:33:34 +0100 | [diff] [blame] | 16 | #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \ |
Augustin Cavalier | 60bc47d | 2018-04-11 20:27:32 -0400 | [diff] [blame] | 17 | !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \ |
Ørjan Malde | 479d8de | 2020-05-20 09:32:39 +0000 | [diff] [blame] | 18 | !defined(__HAIKU__) && !defined(__midipix__) |
Bence Szépkúti | bb0cfeb | 2021-05-28 09:42:25 +0200 | [diff] [blame] | 19 | #error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in mbedtls_config.h" |
Manuel Pégourié-Gonnard | 325ce09 | 2016-02-22 10:33:34 +0100 | [diff] [blame] | 20 | #endif |
| 21 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 22 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 23 | |
| 24 | #include <windows.h> |
irwir | e931d0e | 2018-06-23 18:55:14 +0300 | [diff] [blame] | 25 | #include <process.h> |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 27 | struct _hr_time { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | LARGE_INTEGER start; |
| 29 | }; |
| 30 | |
| 31 | #else |
| 32 | |
| 33 | #include <unistd.h> |
| 34 | #include <sys/types.h> |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 35 | #include <signal.h> |
Andrzej Kurek | 5735369 | 2022-04-07 08:08:21 -0400 | [diff] [blame] | 36 | /* time.h should be included independently of MBEDTLS_HAVE_TIME. If the |
| 37 | * platform matches the ifdefs above, it will be used. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 38 | #include <time.h> |
Andrzej Kurek | 09e803c | 2022-03-02 11:20:23 -0500 | [diff] [blame] | 39 | #include <sys/time.h> |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 40 | struct _hr_time { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 41 | struct timeval start; |
| 42 | }; |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 43 | #endif /* _WIN32 && !EFIX64 && !EFI32 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 44 | |
TRodziewicz | 963bb81 | 2021-06-18 13:22:57 +0200 | [diff] [blame] | 45 | /** |
| 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 Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 64 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 65 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 67 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 68 | struct _hr_time *t = (struct _hr_time *) val; |
| 69 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 70 | if (reset) { |
| 71 | QueryPerformanceCounter(&t->start); |
| 72 | return 0; |
| 73 | } else { |
Gilles Peskine | d92f0aa | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 74 | unsigned long delta; |
| 75 | LARGE_INTEGER now, hfreq; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 76 | QueryPerformanceCounter(&now); |
| 77 | QueryPerformanceFrequency(&hfreq); |
| 78 | delta = (unsigned long) ((now.QuadPart - t->start.QuadPart) * 1000ul |
| 79 | / hfreq.QuadPart); |
| 80 | return delta; |
Gilles Peskine | d92f0aa | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 81 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 84 | #else /* _WIN32 && !EFIX64 && !EFI32 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 85 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 86 | unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 87 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 88 | struct _hr_time *t = (struct _hr_time *) val; |
| 89 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 90 | if (reset) { |
| 91 | gettimeofday(&t->start, NULL); |
| 92 | return 0; |
| 93 | } else { |
Gilles Peskine | d92f0aa | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 94 | unsigned long delta; |
| 95 | struct timeval now; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 96 | 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 Peskine | d92f0aa | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 100 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 103 | #endif /* _WIN32 && !EFIX64 && !EFI32 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 104 | |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 105 | /* |
| 106 | * Set delays to watch |
| 107 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 108 | void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms) |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 109 | { |
| 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 Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 115 | if (fin_ms != 0) { |
| 116 | (void) mbedtls_timing_get_timer(&ctx->timer, 1); |
| 117 | } |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Get number of delays expired |
| 122 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 123 | int mbedtls_timing_get_delay(void *data) |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 124 | { |
| 125 | mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data; |
| 126 | unsigned long elapsed_ms; |
| 127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 128 | if (ctx->fin_ms == 0) { |
| 129 | return -1; |
| 130 | } |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 132 | elapsed_ms = mbedtls_timing_get_timer(&ctx->timer, 0); |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 134 | if (elapsed_ms >= ctx->fin_ms) { |
| 135 | return 2; |
| 136 | } |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 138 | if (elapsed_ms >= ctx->int_ms) { |
| 139 | return 1; |
| 140 | } |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 142 | return 0; |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 143 | } |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 144 | |
| 145 | /* |
| 146 | * Get the final delay. |
| 147 | */ |
| 148 | uint32_t mbedtls_timing_get_final_delay( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 149 | const mbedtls_timing_delay_context *data) |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 150 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 151 | return data->fin_ms; |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 152 | } |
Manuel Pégourié-Gonnard | 8903fe0 | 2015-05-12 19:30:45 +0200 | [diff] [blame] | 153 | #endif /* !MBEDTLS_TIMING_ALT */ |
Manuel Pégourié-Gonnard | 8903fe0 | 2015-05-12 19:30:45 +0200 | [diff] [blame] | 154 | #endif /* MBEDTLS_TIMING_C */ |