blob: 7f559bebd57bde30c8063bb630049fb27c607af8 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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 Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010023
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +020024#if defined(MBEDTLS_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +020028#if !defined(MBEDTLS_TIMING_ALT)
29
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010030#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040031 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
Ørjan Malde479d8de2020-05-20 09:32:39 +000032 !defined(__HAIKU__) && !defined(__midipix__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010033#error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in config.h"
34#endif
35
David Horstmannb6bf5f52023-01-03 11:07:09 +000036/* *INDENT-OFF* */
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020037#ifndef asm
38#define asm __asm
39#endif
David Horstmannb6bf5f52023-01-03 11:07:09 +000040/* *INDENT-ON* */
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020041
Paul Bakkerfa6a6202013-10-28 18:48:30 +010042#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000043
44#include <windows.h>
irwire931d0e2018-06-23 18:55:14 +030045#include <process.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000046
47struct _hr_time
48{
49 LARGE_INTEGER start;
50};
51
52#else
53
54#include <unistd.h>
55#include <sys/types.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000056#include <signal.h>
Andrzej Kurek263d8f72022-04-08 08:34:41 -040057/* time.h should be included independently of MBEDTLS_HAVE_TIME. If the
58 * platform matches the ifdefs above, it will be used. */
Paul Bakker5121ce52009-01-03 21:22:43 +000059#include <time.h>
Andrzej Kurek77daaad2022-03-04 15:10:06 -050060#include <sys/time.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000061struct _hr_time
62{
63 struct timeval start;
64};
Paul Bakker9af723c2014-05-01 13:03:14 +020065#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020067#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +020068 ( defined(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__)
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020070#define HAVE_HARDCLOCK
Paul Bakkerbb0139c2012-10-31 09:53:08 +000071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +000073{
74 unsigned long tsc;
75 __asm rdtsc
76 __asm mov [tsc], eax
77 return( tsc );
78}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020079#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +020080 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
Paul Bakker5121ce52009-01-03 21:22:43 +000081
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +000082/* some versions of mingw-64 have 32-bit longs even on x84_64 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020083#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +000084 defined(__GNUC__) && ( defined(__i386__) || ( \
85 ( defined(__amd64__) || defined( __x86_64__) ) && __SIZEOF_LONG__ == 4 ) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +000086
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020087#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +000088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +000090{
Paul Bakkerca410102011-10-19 14:27:36 +000091 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +010092 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakkerca410102011-10-19 14:27:36 +000093 return( lo );
Paul Bakker5121ce52009-01-03 21:22:43 +000094}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020095#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +020096 __GNUC__ && __i386__ */
Paul Bakker5121ce52009-01-03 21:22:43 +000097
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020098#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +020099 defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000100
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200101#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000104{
105 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100106 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200107 return( lo | ( hi << 32 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000108}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200109#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200110 __GNUC__ && ( __amd64__ || __x86_64__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200112#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +0200113 defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000114
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200115#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000118{
119 unsigned long tbl, tbu0, tbu1;
120
121 do
122 {
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100123 asm volatile( "mftbu %0" : "=r" (tbu0) );
124 asm volatile( "mftb %0" : "=r" (tbl ) );
125 asm volatile( "mftbu %0" : "=r" (tbu1) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 }
127 while( tbu0 != tbu1 );
128
129 return( tbl );
130}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200131#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200132 __GNUC__ && ( __powerpc__ || __ppc__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000133
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200134#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000135 defined(__GNUC__) && defined(__sparc64__)
136
137#if defined(__OpenBSD__)
138#warning OpenBSD does not allow access to tick register using software version instead
Paul Bakker5121ce52009-01-03 21:22:43 +0000139#else
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200140#define HAVE_HARDCLOCK
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142unsigned long mbedtls_timing_hardclock( void )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000143{
144 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100145 asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) );
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000146 return( tick );
147}
Paul Bakker9af723c2014-05-01 13:03:14 +0200148#endif /* __OpenBSD__ */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200149#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200150 __GNUC__ && __sparc64__ */
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000151
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200152#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000153 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
154
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200155#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000158{
159 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100160 asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" );
161 asm volatile( "mov %%g1, %0" : "=r" (tick) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 return( tick );
163}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200164#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200165 __GNUC__ && __sparc__ && !__sparc64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200167#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000168 defined(__GNUC__) && defined(__alpha__)
169
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200170#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000173{
174 unsigned long cc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100175 asm volatile( "rpcc %0" : "=r" (cc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 return( cc & 0xFFFFFFFF );
177}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200178#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200179 __GNUC__ && __alpha__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000180
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200181#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000182 defined(__GNUC__) && defined(__ia64__)
183
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200184#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000187{
188 unsigned long itc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100189 asm volatile( "mov %0 = ar.itc" : "=r" (itc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 return( itc );
191}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200192#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200193 __GNUC__ && __ia64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200195#if !defined(HAVE_HARDCLOCK) && defined(_MSC_VER) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100196 !defined(EFIX64) && !defined(EFI32)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000197
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200198#define HAVE_HARDCLOCK
Paul Bakker2eee9022011-04-24 15:28:55 +0000199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200unsigned long mbedtls_timing_hardclock( void )
Paul Bakker2eee9022011-04-24 15:28:55 +0000201{
202 LARGE_INTEGER offset;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100203
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100204 QueryPerformanceCounter( &offset );
Paul Bakker2eee9022011-04-24 15:28:55 +0000205
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200206 return( (unsigned long)( offset.QuadPart ) );
Paul Bakker2eee9022011-04-24 15:28:55 +0000207}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200208#endif /* !HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
Paul Bakker2eee9022011-04-24 15:28:55 +0000209
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200210#if !defined(HAVE_HARDCLOCK)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000211
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200212#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
214static int hardclock_init = 0;
215static struct timeval tv_init;
216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000218{
219 struct timeval tv_cur;
220
221 if( hardclock_init == 0 )
222 {
223 gettimeofday( &tv_init, NULL );
224 hardclock_init = 1;
225 }
226
227 gettimeofday( &tv_cur, NULL );
Dave Rodgmanb03c5582022-12-20 13:20:01 +0000228 return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000U
Paul Bakker5121ce52009-01-03 21:22:43 +0000229 + ( tv_cur.tv_usec - tv_init.tv_usec ) );
230}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200231#endif /* !HAVE_HARDCLOCK */
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233volatile int mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100235#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000238{
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 struct _hr_time *t = (struct _hr_time *) val;
240
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 if( reset )
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200242 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 QueryPerformanceCounter( &t->start );
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200244 return( 0 );
245 }
246 else
247 {
248 unsigned long delta;
249 LARGE_INTEGER now, hfreq;
250 QueryPerformanceCounter( &now );
251 QueryPerformanceFrequency( &hfreq );
252 delta = (unsigned long)( ( now.QuadPart - t->start.QuadPart ) * 1000ul
253 / hfreq.QuadPart );
254 return( delta );
255 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000256}
257
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000258/* It's OK to use a global because alarm() is supposed to be global anyway */
259static DWORD alarmMs;
260
irwire931d0e2018-06-23 18:55:14 +0300261static void TimerProc( void *TimerContext )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100262{
irwire1b82ad2018-08-30 11:57:09 +0300263 (void) TimerContext;
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000264 Sleep( alarmMs );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_timing_alarmed = 1;
irwirda642d92018-08-31 15:14:54 +0300266 /* _endthread will be called implicitly on return
Tom Cosgrove49f99bc2022-12-04 16:44:21 +0000267 * That ensures execution of thread function's epilogue */
Paul Bakker5121ce52009-01-03 21:22:43 +0000268}
269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270void mbedtls_set_alarm( int seconds )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100271{
Manuel Pégourié-Gonnard54059622018-01-29 10:16:30 +0100272 if( seconds == 0 )
273 {
274 /* No need to create a thread for this simple case.
275 * Also, this shorcut is more reliable at least on MinGW32 */
276 mbedtls_timing_alarmed = 1;
277 return;
278 }
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_timing_alarmed = 0;
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000281 alarmMs = seconds * 1000;
irwire1b82ad2018-08-30 11:57:09 +0300282 (void) _beginthread( TimerProc, 0, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000283}
284
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100285#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000288{
Paul Bakker5121ce52009-01-03 21:22:43 +0000289 struct _hr_time *t = (struct _hr_time *) val;
290
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 if( reset )
292 {
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200293 gettimeofday( &t->start, NULL );
Alfred Klompb308dd72014-07-14 22:32:21 +0200294 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 }
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200296 else
297 {
298 unsigned long delta;
299 struct timeval now;
300 gettimeofday( &now, NULL );
301 delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul
302 + ( now.tv_usec - t->start.tv_usec ) / 1000;
303 return( delta );
304 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000305}
306
307static void sighandler( int signum )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100308{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 mbedtls_timing_alarmed = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000310 signal( signum, sighandler );
311}
312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313void mbedtls_set_alarm( int seconds )
Paul Bakker5121ce52009-01-03 21:22:43 +0000314{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 signal( SIGALRM, sighandler );
317 alarm( seconds );
Gilles Peskinea0af95f2017-10-10 20:10:46 +0200318 if( seconds == 0 )
319 {
320 /* alarm(0) cancelled any previous pending alarm, but the
321 handler won't fire, so raise the flag straight away. */
322 mbedtls_timing_alarmed = 1;
323 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000324}
325
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100326#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000327
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200328/*
329 * Set delays to watch
330 */
331void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms )
332{
333 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
334
335 ctx->int_ms = int_ms;
336 ctx->fin_ms = fin_ms;
337
338 if( fin_ms != 0 )
339 (void) mbedtls_timing_get_timer( &ctx->timer, 1 );
340}
341
342/*
343 * Get number of delays expired
344 */
345int mbedtls_timing_get_delay( void *data )
346{
347 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
348 unsigned long elapsed_ms;
349
350 if( ctx->fin_ms == 0 )
351 return( -1 );
352
353 elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 );
354
355 if( elapsed_ms >= ctx->fin_ms )
356 return( 2 );
357
358 if( elapsed_ms >= ctx->int_ms )
359 return( 1 );
360
361 return( 0 );
362}
363
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100366
367/*
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200368 * Busy-waits for the given number of milliseconds.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 * Used for testing mbedtls_timing_hardclock.
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200370 */
371static void busy_msleep( unsigned long msec )
372{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200374 unsigned long i = 0; /* for busy-waiting */
375 volatile unsigned long j; /* to prevent optimisation */
376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 while( mbedtls_timing_get_timer( &hires, 0 ) < msec )
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200380 i++;
381
382 j = i;
383 (void) j;
384}
385
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200386#define FAIL do \
387 { \
388 if( verbose != 0 ) \
389 { \
390 mbedtls_printf( "failed at line %d\n", __LINE__ ); \
391 mbedtls_printf( " cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d a=%lu b=%lu\n", \
392 cycles, ratio, millisecs, secs, hardfail, \
393 (unsigned long) a, (unsigned long) b ); \
394 mbedtls_printf( " elapsed(hires)=%lu elapsed(ctx)=%lu status(ctx)=%d\n", \
395 mbedtls_timing_get_timer( &hires, 0 ), \
396 mbedtls_timing_get_timer( &ctx.timer, 0 ), \
397 mbedtls_timing_get_delay( &ctx ) ); \
398 } \
399 return( 1 ); \
400 } while( 0 )
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200401
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200402/*
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100403 * Checkup routine
Manuel Pégourié-Gonnard0f79bab2014-04-09 09:56:16 +0200404 *
405 * Warning: this is work in progress, some tests may not be reliable enough
406 * yet! False positives may happen.
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100407 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408int mbedtls_timing_self_test( int verbose )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100409{
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200410 unsigned long cycles = 0, ratio = 0;
411 unsigned long millisecs = 0, secs = 0;
412 int hardfail = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 struct mbedtls_timing_hr_time hires;
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200414 uint32_t a = 0, b = 0;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200415 mbedtls_timing_delay_context ctx;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100416
Paul Bakker66d5d072014-06-17 16:39:18 +0200417 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 mbedtls_printf( " TIMING tests note: will take some time!\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100419
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100420 if( verbose != 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200421 mbedtls_printf( " TIMING test #1 (set_alarm / get_timer): " );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100422
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100423 {
Gilles Peskineada3ee82017-12-20 22:31:17 +0100424 secs = 1;
425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_set_alarm( (int) secs );
429 while( !mbedtls_timing_alarmed )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100430 ;
431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 millisecs = mbedtls_timing_get_timer( &hires, 0 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100433
Manuel Pégourié-Gonnarde578b1c2015-08-18 20:11:48 +0200434 /* For some reason on Windows it looks like alarm has an extra delay
435 * (maybe related to creating a new thread). Allow some room here. */
436 if( millisecs < 800 * secs || millisecs > 1200 * secs + 300 )
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200437 FAIL;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100438 }
439
440 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100442
443 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200444 mbedtls_printf( " TIMING test #2 (set/get_delay ): " );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200445
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200446 {
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200447 a = 800;
448 b = 400;
449 mbedtls_timing_set_delay( &ctx, a, a + b ); /* T = 0 */
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200450
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200451 busy_msleep( a - a / 4 ); /* T = a - a/4 */
452 if( mbedtls_timing_get_delay( &ctx ) != 0 )
453 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200454
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200455 busy_msleep( a / 4 + b / 4 ); /* T = a + b/4 */
456 if( mbedtls_timing_get_delay( &ctx ) != 1 )
457 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200458
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200459 busy_msleep( b ); /* T = a + b + b/4 */
460 if( mbedtls_timing_get_delay( &ctx ) != 2 )
461 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200462 }
463
464 mbedtls_timing_set_delay( &ctx, 0, 0 );
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200465 busy_msleep( 200 );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200466 if( mbedtls_timing_get_delay( &ctx ) != -1 )
467 FAIL;
468
469 if( verbose != 0 )
470 mbedtls_printf( "passed\n" );
471
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200472 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200473 mbedtls_printf( " TIMING test #3 (hardclock / get_timer): " );
474
475 /*
476 * Allow one failure for possible counter wrapping.
477 * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
478 * since the whole test is about 10ms, it shouldn't happen twice in a row.
479 */
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200480
481hard_test:
482 if( hardfail > 1 )
483 {
484 if( verbose != 0 )
485 mbedtls_printf( "failed (ignored)\n" );
486
487 goto hard_test_done;
488 }
489
490 /* Get a reference ratio cycles/ms */
491 millisecs = 1;
492 cycles = mbedtls_timing_hardclock();
493 busy_msleep( millisecs );
494 cycles = mbedtls_timing_hardclock() - cycles;
495 ratio = cycles / millisecs;
496
497 /* Check that the ratio is mostly constant */
498 for( millisecs = 2; millisecs <= 4; millisecs++ )
499 {
500 cycles = mbedtls_timing_hardclock();
501 busy_msleep( millisecs );
502 cycles = mbedtls_timing_hardclock() - cycles;
503
504 /* Allow variation up to 20% */
505 if( cycles / millisecs < ratio - ratio / 5 ||
506 cycles / millisecs > ratio + ratio / 5 )
507 {
508 hardfail++;
509 goto hard_test;
510 }
511 }
512
513 if( verbose != 0 )
514 mbedtls_printf( "passed\n" );
515
516hard_test_done:
517
518 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200520
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100521 return( 0 );
522}
523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#endif /* MBEDTLS_SELF_TEST */
Andrzej Kurek77daaad2022-03-04 15:10:06 -0500525#endif /* !MBEDTLS_TIMING_ALT */
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200526#endif /* MBEDTLS_TIMING_C */