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 | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 23 | #include "mbedtls/platform.h" |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 24 | #else |
| 25 | #include <stdio.h> |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #define mbedtls_printf printf |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 27 | #endif |
| 28 | |
Manuel Pégourié-Gonnard | 8903fe0 | 2015-05-12 19:30:45 +0200 | [diff] [blame] | 29 | #if defined(MBEDTLS_TIMING_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 30 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/timing.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 32 | |
Manuel Pégourié-Gonnard | 8903fe0 | 2015-05-12 19:30:45 +0200 | [diff] [blame] | 33 | #if !defined(MBEDTLS_TIMING_ALT) |
| 34 | |
Manuel Pégourié-Gonnard | 325ce09 | 2016-02-22 10:33:34 +0100 | [diff] [blame] | 35 | #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \ |
Augustin Cavalier | 60bc47d | 2018-04-11 20:27:32 -0400 | [diff] [blame] | 36 | !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \ |
Ørjan Malde | 479d8de | 2020-05-20 09:32:39 +0000 | [diff] [blame] | 37 | !defined(__HAIKU__) && !defined(__midipix__) |
Manuel Pégourié-Gonnard | 325ce09 | 2016-02-22 10:33:34 +0100 | [diff] [blame] | 38 | #error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in config.h" |
| 39 | #endif |
| 40 | |
Manuel Pégourié-Gonnard | ba19432 | 2015-05-29 09:47:57 +0200 | [diff] [blame] | 41 | #ifndef asm |
| 42 | #define asm __asm |
| 43 | #endif |
| 44 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 45 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 46 | |
| 47 | #include <windows.h> |
irwir | e931d0e | 2018-06-23 18:55:14 +0300 | [diff] [blame] | 48 | #include <process.h> |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 49 | |
| 50 | struct _hr_time |
| 51 | { |
| 52 | LARGE_INTEGER start; |
| 53 | }; |
| 54 | |
| 55 | #else |
| 56 | |
| 57 | #include <unistd.h> |
| 58 | #include <sys/types.h> |
| 59 | #include <sys/time.h> |
| 60 | #include <signal.h> |
| 61 | #include <time.h> |
| 62 | |
| 63 | struct _hr_time |
| 64 | { |
| 65 | struct timeval start; |
| 66 | }; |
| 67 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 68 | #endif /* _WIN32 && !EFIX64 && !EFI32 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 69 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 70 | volatile int mbedtls_timing_alarmed = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 71 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 72 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 73 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 74 | 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] | 75 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 76 | struct _hr_time *t = (struct _hr_time *) val; |
| 77 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 78 | if( reset ) |
Gilles Peskine | d92f0aa | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 79 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 80 | QueryPerformanceCounter( &t->start ); |
Gilles Peskine | d92f0aa | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 81 | return( 0 ); |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | unsigned long delta; |
| 86 | LARGE_INTEGER now, hfreq; |
| 87 | QueryPerformanceCounter( &now ); |
| 88 | QueryPerformanceFrequency( &hfreq ); |
| 89 | delta = (unsigned long)( ( now.QuadPart - t->start.QuadPart ) * 1000ul |
| 90 | / hfreq.QuadPart ); |
| 91 | return( delta ); |
| 92 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 95 | #else /* _WIN32 && !EFIX64 && !EFI32 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 96 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 97 | 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] | 98 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 99 | struct _hr_time *t = (struct _hr_time *) val; |
| 100 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 101 | if( reset ) |
| 102 | { |
Gilles Peskine | d92f0aa | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 103 | gettimeofday( &t->start, NULL ); |
Alfred Klomp | b308dd7 | 2014-07-14 22:32:21 +0200 | [diff] [blame] | 104 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 105 | } |
Gilles Peskine | d92f0aa | 2017-10-16 19:33:06 +0200 | [diff] [blame] | 106 | else |
| 107 | { |
| 108 | unsigned long delta; |
| 109 | struct timeval now; |
| 110 | gettimeofday( &now, NULL ); |
| 111 | delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul |
| 112 | + ( now.tv_usec - t->start.tv_usec ) / 1000; |
| 113 | return( delta ); |
| 114 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 117 | #endif /* _WIN32 && !EFIX64 && !EFI32 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 118 | |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 119 | /* |
| 120 | * Set delays to watch |
| 121 | */ |
| 122 | void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms ) |
| 123 | { |
| 124 | mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data; |
| 125 | |
| 126 | ctx->int_ms = int_ms; |
| 127 | ctx->fin_ms = fin_ms; |
| 128 | |
| 129 | if( fin_ms != 0 ) |
| 130 | (void) mbedtls_timing_get_timer( &ctx->timer, 1 ); |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Get number of delays expired |
| 135 | */ |
| 136 | int mbedtls_timing_get_delay( void *data ) |
| 137 | { |
| 138 | mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data; |
| 139 | unsigned long elapsed_ms; |
| 140 | |
| 141 | if( ctx->fin_ms == 0 ) |
| 142 | return( -1 ); |
| 143 | |
| 144 | elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 ); |
| 145 | |
| 146 | if( elapsed_ms >= ctx->fin_ms ) |
| 147 | return( 2 ); |
| 148 | |
| 149 | if( elapsed_ms >= ctx->int_ms ) |
| 150 | return( 1 ); |
| 151 | |
| 152 | return( 0 ); |
| 153 | } |
| 154 | |
Manuel Pégourié-Gonnard | 8903fe0 | 2015-05-12 19:30:45 +0200 | [diff] [blame] | 155 | #endif /* !MBEDTLS_TIMING_ALT */ |
| 156 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 157 | #if defined(MBEDTLS_SELF_TEST) |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 158 | |
Gilles Peskine | 0827d5c | 2017-10-10 20:09:26 +0200 | [diff] [blame] | 159 | #define FAIL do \ |
| 160 | { \ |
| 161 | if( verbose != 0 ) \ |
| 162 | { \ |
| 163 | mbedtls_printf( "failed at line %d\n", __LINE__ ); \ |
| 164 | mbedtls_printf( " cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d a=%lu b=%lu\n", \ |
| 165 | cycles, ratio, millisecs, secs, hardfail, \ |
| 166 | (unsigned long) a, (unsigned long) b ); \ |
Gilles Peskine | d41d59e | 2021-05-25 09:19:45 +0200 | [diff] [blame] | 167 | print_timers( &hires, &ctx ); \ |
Gilles Peskine | 0827d5c | 2017-10-10 20:09:26 +0200 | [diff] [blame] | 168 | } \ |
| 169 | return( 1 ); \ |
| 170 | } while( 0 ) |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 171 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 172 | #endif /* MBEDTLS_SELF_TEST */ |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 173 | |
Manuel Pégourié-Gonnard | 8903fe0 | 2015-05-12 19:30:45 +0200 | [diff] [blame] | 174 | #endif /* MBEDTLS_TIMING_C */ |