| 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 | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | *  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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 18 | */ | 
|  | 19 |  | 
| Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 20 | #include "common.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 21 |  | 
| Manuel Pégourié-Gonnard | 8903fe0 | 2015-05-12 19:30:45 +0200 | [diff] [blame] | 22 | #if defined(MBEDTLS_TIMING_C) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 23 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/timing.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 25 |  | 
| Manuel Pégourié-Gonnard | 8903fe0 | 2015-05-12 19:30:45 +0200 | [diff] [blame] | 26 | #if !defined(MBEDTLS_TIMING_ALT) | 
|  | 27 |  | 
| Manuel Pégourié-Gonnard | 325ce09 | 2016-02-22 10:33:34 +0100 | [diff] [blame] | 28 | #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \ | 
| Augustin Cavalier | 60bc47d | 2018-04-11 20:27:32 -0400 | [diff] [blame] | 29 | !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \ | 
| Ørjan Malde | 479d8de | 2020-05-20 09:32:39 +0000 | [diff] [blame] | 30 | !defined(__HAIKU__) && !defined(__midipix__) | 
| Bence Szépkúti | bb0cfeb | 2021-05-28 09:42:25 +0200 | [diff] [blame] | 31 | #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] | 32 | #endif | 
|  | 33 |  | 
| Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 34 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 35 |  | 
|  | 36 | #include <windows.h> | 
| irwir | e931d0e | 2018-06-23 18:55:14 +0300 | [diff] [blame] | 37 | #include <process.h> | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 38 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 39 | struct _hr_time { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 40 | LARGE_INTEGER start; | 
|  | 41 | }; | 
|  | 42 |  | 
|  | 43 | #else | 
|  | 44 |  | 
|  | 45 | #include <unistd.h> | 
|  | 46 | #include <sys/types.h> | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 47 | #include <signal.h> | 
| Andrzej Kurek | 5735369 | 2022-04-07 08:08:21 -0400 | [diff] [blame] | 48 | /* time.h should be included independently of MBEDTLS_HAVE_TIME. If the | 
|  | 49 | * platform matches the ifdefs above, it will be used. */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 50 | #include <time.h> | 
| Andrzej Kurek | 09e803c | 2022-03-02 11:20:23 -0500 | [diff] [blame] | 51 | #include <sys/time.h> | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 52 | struct _hr_time { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 53 | struct timeval start; | 
|  | 54 | }; | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 55 | #endif /* _WIN32 && !EFIX64 && !EFI32 */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 56 |  | 
| TRodziewicz | 963bb81 | 2021-06-18 13:22:57 +0200 | [diff] [blame] | 57 | /** | 
|  | 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 Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 76 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 77 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 78 | 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] | 79 | { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 80 | struct _hr_time *t = (struct _hr_time *) val; | 
|  | 81 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 82 | if (reset) { | 
|  | 83 | QueryPerformanceCounter(&t->start); | 
|  | 84 | return 0; | 
|  | 85 | } else { | 
| Gilles Peskine | d92f0aa | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 86 | unsigned long delta; | 
|  | 87 | LARGE_INTEGER now, hfreq; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 88 | QueryPerformanceCounter(&now); | 
|  | 89 | QueryPerformanceFrequency(&hfreq); | 
|  | 90 | delta = (unsigned long) ((now.QuadPart - t->start.QuadPart) * 1000ul | 
|  | 91 | / hfreq.QuadPart); | 
|  | 92 | return delta; | 
| Gilles Peskine | d92f0aa | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 93 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 94 | } | 
|  | 95 |  | 
| Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 96 | #else /* _WIN32 && !EFIX64 && !EFI32 */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 97 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 98 | 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] | 99 | { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 100 | struct _hr_time *t = (struct _hr_time *) val; | 
|  | 101 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 102 | if (reset) { | 
|  | 103 | gettimeofday(&t->start, NULL); | 
|  | 104 | return 0; | 
|  | 105 | } else { | 
| Gilles Peskine | d92f0aa | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 106 | unsigned long delta; | 
|  | 107 | struct timeval now; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 108 | 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 Peskine | d92f0aa | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 112 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 113 | } | 
|  | 114 |  | 
| Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 115 | #endif /* _WIN32 && !EFIX64 && !EFI32 */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 116 |  | 
| Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 117 | /* | 
|  | 118 | * Set delays to watch | 
|  | 119 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 120 | 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] | 121 | { | 
|  | 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 Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 127 | if (fin_ms != 0) { | 
|  | 128 | (void) mbedtls_timing_get_timer(&ctx->timer, 1); | 
|  | 129 | } | 
| Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 130 | } | 
|  | 131 |  | 
|  | 132 | /* | 
|  | 133 | * Get number of delays expired | 
|  | 134 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 135 | int mbedtls_timing_get_delay(void *data) | 
| Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 136 | { | 
|  | 137 | mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data; | 
|  | 138 | unsigned long elapsed_ms; | 
|  | 139 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 140 | if (ctx->fin_ms == 0) { | 
|  | 141 | return -1; | 
|  | 142 | } | 
| Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 143 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 144 | elapsed_ms = mbedtls_timing_get_timer(&ctx->timer, 0); | 
| Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 145 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 146 | if (elapsed_ms >= ctx->fin_ms) { | 
|  | 147 | return 2; | 
|  | 148 | } | 
| Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 149 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 150 | if (elapsed_ms >= ctx->int_ms) { | 
|  | 151 | return 1; | 
|  | 152 | } | 
| Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 153 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 154 | return 0; | 
| Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 155 | } | 
| Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 156 |  | 
|  | 157 | /* | 
|  | 158 | * Get the final delay. | 
|  | 159 | */ | 
|  | 160 | uint32_t mbedtls_timing_get_final_delay( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 161 | const mbedtls_timing_delay_context *data) | 
| Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 162 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 163 | return data->fin_ms; | 
| Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 164 | } | 
| Manuel Pégourié-Gonnard | 8903fe0 | 2015-05-12 19:30:45 +0200 | [diff] [blame] | 165 | #endif /* !MBEDTLS_TIMING_ALT */ | 
| Manuel Pégourié-Gonnard | 8903fe0 | 2015-05-12 19:30:45 +0200 | [diff] [blame] | 166 | #endif /* MBEDTLS_TIMING_C */ |