| 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 |  | 
|  | 39 | struct _hr_time | 
|  | 40 | { | 
|  | 41 | LARGE_INTEGER start; | 
|  | 42 | }; | 
|  | 43 |  | 
|  | 44 | #else | 
|  | 45 |  | 
|  | 46 | #include <unistd.h> | 
|  | 47 | #include <sys/types.h> | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 48 | #include <signal.h> | 
| Andrzej Kurek | 5735369 | 2022-04-07 08:08:21 -0400 | [diff] [blame] | 49 | /* time.h should be included independently of MBEDTLS_HAVE_TIME. If the | 
|  | 50 | * platform matches the ifdefs above, it will be used. */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 51 | #include <time.h> | 
| Andrzej Kurek | 09e803c | 2022-03-02 11:20:23 -0500 | [diff] [blame] | 52 | #include <sys/time.h> | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 53 | struct _hr_time | 
|  | 54 | { | 
|  | 55 | struct timeval start; | 
|  | 56 | }; | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 57 | #endif /* _WIN32 && !EFIX64 && !EFI32 */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 58 |  | 
| TRodziewicz | 963bb81 | 2021-06-18 13:22:57 +0200 | [diff] [blame] | 59 | /** | 
|  | 60 | * \brief          Return the elapsed time in milliseconds | 
|  | 61 | * | 
|  | 62 | * \warning        May change without notice | 
|  | 63 | * | 
|  | 64 | * \param val      points to a timer structure | 
|  | 65 | * \param reset    If 0, query the elapsed time. Otherwise (re)start the timer. | 
|  | 66 | * | 
|  | 67 | * \return         Elapsed time since the previous reset in ms. When | 
|  | 68 | *                 restarting, this is always 0. | 
|  | 69 | * | 
|  | 70 | * \note           To initialize a timer, call this function with reset=1. | 
|  | 71 | * | 
|  | 72 | *                 Determining the elapsed time and resetting the timer is not | 
|  | 73 | *                 atomic on all platforms, so after the sequence | 
|  | 74 | *                 `{ get_timer(1); ...; time1 = get_timer(1); ...; time2 = | 
|  | 75 | *                 get_timer(0) }` the value time1+time2 is only approximately | 
|  | 76 | *                 the delay since the first reset. | 
|  | 77 | */ | 
| Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 78 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 79 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 80 | 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] | 81 | { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 82 | struct _hr_time *t = (struct _hr_time *) val; | 
|  | 83 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 84 | if( reset ) | 
| Gilles Peskine | d92f0aa | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 85 | { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 86 | QueryPerformanceCounter( &t->start ); | 
| Gilles Peskine | d92f0aa | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 87 | return( 0 ); | 
|  | 88 | } | 
|  | 89 | else | 
|  | 90 | { | 
|  | 91 | unsigned long delta; | 
|  | 92 | LARGE_INTEGER now, hfreq; | 
|  | 93 | QueryPerformanceCounter(  &now ); | 
|  | 94 | QueryPerformanceFrequency( &hfreq ); | 
|  | 95 | delta = (unsigned long)( ( now.QuadPart - t->start.QuadPart ) * 1000ul | 
|  | 96 | / hfreq.QuadPart ); | 
|  | 97 | return( delta ); | 
|  | 98 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 99 | } | 
|  | 100 |  | 
| Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 101 | #else /* _WIN32 && !EFIX64 && !EFI32 */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 102 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 103 | 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] | 104 | { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 105 | struct _hr_time *t = (struct _hr_time *) val; | 
|  | 106 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 107 | if( reset ) | 
|  | 108 | { | 
| Gilles Peskine | d92f0aa | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 109 | gettimeofday( &t->start, NULL ); | 
| Alfred Klomp | b308dd7 | 2014-07-14 22:32:21 +0200 | [diff] [blame] | 110 | return( 0 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 111 | } | 
| Gilles Peskine | d92f0aa | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 112 | else | 
|  | 113 | { | 
|  | 114 | unsigned long delta; | 
|  | 115 | struct timeval now; | 
|  | 116 | gettimeofday( &now, NULL ); | 
|  | 117 | delta = ( now.tv_sec  - t->start.tv_sec  ) * 1000ul | 
|  | 118 | + ( now.tv_usec - t->start.tv_usec ) / 1000; | 
|  | 119 | return( delta ); | 
|  | 120 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 121 | } | 
|  | 122 |  | 
| Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 123 | #endif /* _WIN32 && !EFIX64 && !EFI32 */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 124 |  | 
| Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 125 | /* | 
|  | 126 | * Set delays to watch | 
|  | 127 | */ | 
|  | 128 | void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms ) | 
|  | 129 | { | 
|  | 130 | mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data; | 
|  | 131 |  | 
|  | 132 | ctx->int_ms = int_ms; | 
|  | 133 | ctx->fin_ms = fin_ms; | 
|  | 134 |  | 
|  | 135 | if( fin_ms != 0 ) | 
|  | 136 | (void) mbedtls_timing_get_timer( &ctx->timer, 1 ); | 
|  | 137 | } | 
|  | 138 |  | 
|  | 139 | /* | 
|  | 140 | * Get number of delays expired | 
|  | 141 | */ | 
|  | 142 | int mbedtls_timing_get_delay( void *data ) | 
|  | 143 | { | 
|  | 144 | mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data; | 
|  | 145 | unsigned long elapsed_ms; | 
|  | 146 |  | 
|  | 147 | if( ctx->fin_ms == 0 ) | 
|  | 148 | return( -1 ); | 
|  | 149 |  | 
|  | 150 | elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 ); | 
|  | 151 |  | 
|  | 152 | if( elapsed_ms >= ctx->fin_ms ) | 
|  | 153 | return( 2 ); | 
|  | 154 |  | 
|  | 155 | if( elapsed_ms >= ctx->int_ms ) | 
|  | 156 | return( 1 ); | 
|  | 157 |  | 
|  | 158 | return( 0 ); | 
|  | 159 | } | 
| Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 160 |  | 
|  | 161 | /* | 
|  | 162 | * Get the final delay. | 
|  | 163 | */ | 
|  | 164 | uint32_t mbedtls_timing_get_final_delay( | 
|  | 165 | const mbedtls_timing_delay_context *data ) | 
|  | 166 | { | 
|  | 167 | return( data->fin_ms ); | 
|  | 168 | } | 
| Manuel Pégourié-Gonnard | 8903fe0 | 2015-05-12 19:30:45 +0200 | [diff] [blame] | 169 | #endif /* !MBEDTLS_TIMING_ALT */ | 
| Manuel Pégourié-Gonnard | 8903fe0 | 2015-05-12 19:30:45 +0200 | [diff] [blame] | 170 | #endif /* MBEDTLS_TIMING_C */ |