blob: 6c14a4fd0169e5ac3f4a6cf07c0a8b6c96aee4c8 [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
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020036#ifndef asm
37#define asm __asm
38#endif
39
Paul Bakkerfa6a6202013-10-28 18:48:30 +010040#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
42#include <windows.h>
irwire931d0e2018-06-23 18:55:14 +030043#include <process.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000044
45struct _hr_time
46{
47 LARGE_INTEGER start;
48};
49
50#else
51
52#include <unistd.h>
53#include <sys/types.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000054#include <signal.h>
Andrzej Kurek263d8f72022-04-08 08:34:41 -040055/* time.h should be included independently of MBEDTLS_HAVE_TIME. If the
56 * platform matches the ifdefs above, it will be used. */
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <time.h>
Andrzej Kurek77daaad2022-03-04 15:10:06 -050058#include <sys/time.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000059struct _hr_time
60{
61 struct timeval start;
62};
Paul Bakker9af723c2014-05-01 13:03:14 +020063#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000064
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020065#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +020066 ( defined(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__)
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020068#define HAVE_HARDCLOCK
Paul Bakkerbb0139c2012-10-31 09:53:08 +000069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +000071{
72 unsigned long tsc;
73 __asm rdtsc
74 __asm mov [tsc], eax
75 return( tsc );
76}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020077#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +020078 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
Paul Bakker5121ce52009-01-03 21:22:43 +000079
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +000080/* some versions of mingw-64 have 32-bit longs even on x84_64 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020081#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +000082 defined(__GNUC__) && ( defined(__i386__) || ( \
83 ( defined(__amd64__) || defined( __x86_64__) ) && __SIZEOF_LONG__ == 4 ) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +000084
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020085#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +000086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +000088{
Paul Bakkerca410102011-10-19 14:27:36 +000089 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +010090 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakkerca410102011-10-19 14:27:36 +000091 return( lo );
Paul Bakker5121ce52009-01-03 21:22:43 +000092}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020093#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +020094 __GNUC__ && __i386__ */
Paul Bakker5121ce52009-01-03 21:22:43 +000095
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020096#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +020097 defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +000098
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020099#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000102{
103 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100104 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200105 return( lo | ( hi << 32 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000106}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200107#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200108 __GNUC__ && ( __amd64__ || __x86_64__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200110#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +0200111 defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000112
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200113#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000116{
117 unsigned long tbl, tbu0, tbu1;
118
119 do
120 {
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100121 asm volatile( "mftbu %0" : "=r" (tbu0) );
122 asm volatile( "mftb %0" : "=r" (tbl ) );
123 asm volatile( "mftbu %0" : "=r" (tbu1) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 }
125 while( tbu0 != tbu1 );
126
127 return( tbl );
128}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200129#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200130 __GNUC__ && ( __powerpc__ || __ppc__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200132#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000133 defined(__GNUC__) && defined(__sparc64__)
134
135#if defined(__OpenBSD__)
136#warning OpenBSD does not allow access to tick register using software version instead
Paul Bakker5121ce52009-01-03 21:22:43 +0000137#else
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200138#define HAVE_HARDCLOCK
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140unsigned long mbedtls_timing_hardclock( void )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000141{
142 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100143 asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) );
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000144 return( tick );
145}
Paul Bakker9af723c2014-05-01 13:03:14 +0200146#endif /* __OpenBSD__ */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200147#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200148 __GNUC__ && __sparc64__ */
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000149
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200150#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000151 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
152
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200153#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000156{
157 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100158 asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" );
159 asm volatile( "mov %%g1, %0" : "=r" (tick) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 return( tick );
161}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200162#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200163 __GNUC__ && __sparc__ && !__sparc64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200165#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000166 defined(__GNUC__) && defined(__alpha__)
167
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200168#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000171{
172 unsigned long cc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100173 asm volatile( "rpcc %0" : "=r" (cc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 return( cc & 0xFFFFFFFF );
175}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200176#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200177 __GNUC__ && __alpha__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000178
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200179#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000180 defined(__GNUC__) && defined(__ia64__)
181
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200182#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000185{
186 unsigned long itc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100187 asm volatile( "mov %0 = ar.itc" : "=r" (itc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 return( itc );
189}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200190#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200191 __GNUC__ && __ia64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000192
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200193#if !defined(HAVE_HARDCLOCK) && defined(_MSC_VER) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100194 !defined(EFIX64) && !defined(EFI32)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000195
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200196#define HAVE_HARDCLOCK
Paul Bakker2eee9022011-04-24 15:28:55 +0000197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198unsigned long mbedtls_timing_hardclock( void )
Paul Bakker2eee9022011-04-24 15:28:55 +0000199{
200 LARGE_INTEGER offset;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100201
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100202 QueryPerformanceCounter( &offset );
Paul Bakker2eee9022011-04-24 15:28:55 +0000203
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200204 return( (unsigned long)( offset.QuadPart ) );
Paul Bakker2eee9022011-04-24 15:28:55 +0000205}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200206#endif /* !HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
Paul Bakker2eee9022011-04-24 15:28:55 +0000207
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200208#if !defined(HAVE_HARDCLOCK)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000209
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200210#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
212static int hardclock_init = 0;
213static struct timeval tv_init;
214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000216{
217 struct timeval tv_cur;
218
219 if( hardclock_init == 0 )
220 {
221 gettimeofday( &tv_init, NULL );
222 hardclock_init = 1;
223 }
224
225 gettimeofday( &tv_cur, NULL );
226 return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
227 + ( tv_cur.tv_usec - tv_init.tv_usec ) );
228}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200229#endif /* !HAVE_HARDCLOCK */
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231volatile int mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100233#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000236{
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 struct _hr_time *t = (struct _hr_time *) val;
238
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 if( reset )
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200240 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 QueryPerformanceCounter( &t->start );
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200242 return( 0 );
243 }
244 else
245 {
246 unsigned long delta;
247 LARGE_INTEGER now, hfreq;
248 QueryPerformanceCounter( &now );
249 QueryPerformanceFrequency( &hfreq );
250 delta = (unsigned long)( ( now.QuadPart - t->start.QuadPart ) * 1000ul
251 / hfreq.QuadPart );
252 return( delta );
253 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000254}
255
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000256/* It's OK to use a global because alarm() is supposed to be global anyway */
257static DWORD alarmMs;
258
irwire931d0e2018-06-23 18:55:14 +0300259static void TimerProc( void *TimerContext )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100260{
irwire1b82ad2018-08-30 11:57:09 +0300261 (void) TimerContext;
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000262 Sleep( alarmMs );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 mbedtls_timing_alarmed = 1;
irwirda642d92018-08-31 15:14:54 +0300264 /* _endthread will be called implicitly on return
Tom Cosgrove49f99bc2022-12-04 16:44:21 +0000265 * That ensures execution of thread function's epilogue */
Paul Bakker5121ce52009-01-03 21:22:43 +0000266}
267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268void mbedtls_set_alarm( int seconds )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100269{
Manuel Pégourié-Gonnard54059622018-01-29 10:16:30 +0100270 if( seconds == 0 )
271 {
272 /* No need to create a thread for this simple case.
273 * Also, this shorcut is more reliable at least on MinGW32 */
274 mbedtls_timing_alarmed = 1;
275 return;
276 }
277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 mbedtls_timing_alarmed = 0;
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000279 alarmMs = seconds * 1000;
irwire1b82ad2018-08-30 11:57:09 +0300280 (void) _beginthread( TimerProc, 0, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000281}
282
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100283#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000286{
Paul Bakker5121ce52009-01-03 21:22:43 +0000287 struct _hr_time *t = (struct _hr_time *) val;
288
Paul Bakker5121ce52009-01-03 21:22:43 +0000289 if( reset )
290 {
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200291 gettimeofday( &t->start, NULL );
Alfred Klompb308dd72014-07-14 22:32:21 +0200292 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000293 }
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200294 else
295 {
296 unsigned long delta;
297 struct timeval now;
298 gettimeofday( &now, NULL );
299 delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul
300 + ( now.tv_usec - t->start.tv_usec ) / 1000;
301 return( delta );
302 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000303}
304
305static void sighandler( int signum )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100306{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 mbedtls_timing_alarmed = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000308 signal( signum, sighandler );
309}
310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311void mbedtls_set_alarm( int seconds )
Paul Bakker5121ce52009-01-03 21:22:43 +0000312{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 signal( SIGALRM, sighandler );
315 alarm( seconds );
Gilles Peskinea0af95f2017-10-10 20:10:46 +0200316 if( seconds == 0 )
317 {
318 /* alarm(0) cancelled any previous pending alarm, but the
319 handler won't fire, so raise the flag straight away. */
320 mbedtls_timing_alarmed = 1;
321 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000322}
323
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100324#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200326/*
327 * Set delays to watch
328 */
329void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms )
330{
331 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
332
333 ctx->int_ms = int_ms;
334 ctx->fin_ms = fin_ms;
335
336 if( fin_ms != 0 )
337 (void) mbedtls_timing_get_timer( &ctx->timer, 1 );
338}
339
340/*
341 * Get number of delays expired
342 */
343int mbedtls_timing_get_delay( void *data )
344{
345 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
346 unsigned long elapsed_ms;
347
348 if( ctx->fin_ms == 0 )
349 return( -1 );
350
351 elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 );
352
353 if( elapsed_ms >= ctx->fin_ms )
354 return( 2 );
355
356 if( elapsed_ms >= ctx->int_ms )
357 return( 1 );
358
359 return( 0 );
360}
361
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100364
365/*
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200366 * Busy-waits for the given number of milliseconds.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 * Used for testing mbedtls_timing_hardclock.
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200368 */
369static void busy_msleep( unsigned long msec )
370{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200372 unsigned long i = 0; /* for busy-waiting */
373 volatile unsigned long j; /* to prevent optimisation */
374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 while( mbedtls_timing_get_timer( &hires, 0 ) < msec )
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200378 i++;
379
380 j = i;
381 (void) j;
382}
383
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200384#define FAIL do \
385 { \
386 if( verbose != 0 ) \
387 { \
388 mbedtls_printf( "failed at line %d\n", __LINE__ ); \
389 mbedtls_printf( " cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d a=%lu b=%lu\n", \
390 cycles, ratio, millisecs, secs, hardfail, \
391 (unsigned long) a, (unsigned long) b ); \
392 mbedtls_printf( " elapsed(hires)=%lu elapsed(ctx)=%lu status(ctx)=%d\n", \
393 mbedtls_timing_get_timer( &hires, 0 ), \
394 mbedtls_timing_get_timer( &ctx.timer, 0 ), \
395 mbedtls_timing_get_delay( &ctx ) ); \
396 } \
397 return( 1 ); \
398 } while( 0 )
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200399
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200400/*
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100401 * Checkup routine
Manuel Pégourié-Gonnard0f79bab2014-04-09 09:56:16 +0200402 *
403 * Warning: this is work in progress, some tests may not be reliable enough
404 * yet! False positives may happen.
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406int mbedtls_timing_self_test( int verbose )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100407{
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200408 unsigned long cycles = 0, ratio = 0;
409 unsigned long millisecs = 0, secs = 0;
410 int hardfail = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 struct mbedtls_timing_hr_time hires;
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200412 uint32_t a = 0, b = 0;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200413 mbedtls_timing_delay_context ctx;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100414
Paul Bakker66d5d072014-06-17 16:39:18 +0200415 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_printf( " TIMING tests note: will take some time!\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100417
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100418 if( verbose != 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200419 mbedtls_printf( " TIMING test #1 (set_alarm / get_timer): " );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100420
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100421 {
Gilles Peskineada3ee82017-12-20 22:31:17 +0100422 secs = 1;
423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_set_alarm( (int) secs );
427 while( !mbedtls_timing_alarmed )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100428 ;
429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 millisecs = mbedtls_timing_get_timer( &hires, 0 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100431
Manuel Pégourié-Gonnarde578b1c2015-08-18 20:11:48 +0200432 /* For some reason on Windows it looks like alarm has an extra delay
433 * (maybe related to creating a new thread). Allow some room here. */
434 if( millisecs < 800 * secs || millisecs > 1200 * secs + 300 )
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200435 FAIL;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100436 }
437
438 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100440
441 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200442 mbedtls_printf( " TIMING test #2 (set/get_delay ): " );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200443
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200444 {
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200445 a = 800;
446 b = 400;
447 mbedtls_timing_set_delay( &ctx, a, a + b ); /* T = 0 */
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200448
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200449 busy_msleep( a - a / 4 ); /* T = a - a/4 */
450 if( mbedtls_timing_get_delay( &ctx ) != 0 )
451 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200452
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200453 busy_msleep( a / 4 + b / 4 ); /* T = a + b/4 */
454 if( mbedtls_timing_get_delay( &ctx ) != 1 )
455 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200456
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200457 busy_msleep( b ); /* T = a + b + b/4 */
458 if( mbedtls_timing_get_delay( &ctx ) != 2 )
459 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200460 }
461
462 mbedtls_timing_set_delay( &ctx, 0, 0 );
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200463 busy_msleep( 200 );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200464 if( mbedtls_timing_get_delay( &ctx ) != -1 )
465 FAIL;
466
467 if( verbose != 0 )
468 mbedtls_printf( "passed\n" );
469
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200470 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200471 mbedtls_printf( " TIMING test #3 (hardclock / get_timer): " );
472
473 /*
474 * Allow one failure for possible counter wrapping.
475 * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
476 * since the whole test is about 10ms, it shouldn't happen twice in a row.
477 */
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200478
479hard_test:
480 if( hardfail > 1 )
481 {
482 if( verbose != 0 )
483 mbedtls_printf( "failed (ignored)\n" );
484
485 goto hard_test_done;
486 }
487
488 /* Get a reference ratio cycles/ms */
489 millisecs = 1;
490 cycles = mbedtls_timing_hardclock();
491 busy_msleep( millisecs );
492 cycles = mbedtls_timing_hardclock() - cycles;
493 ratio = cycles / millisecs;
494
495 /* Check that the ratio is mostly constant */
496 for( millisecs = 2; millisecs <= 4; millisecs++ )
497 {
498 cycles = mbedtls_timing_hardclock();
499 busy_msleep( millisecs );
500 cycles = mbedtls_timing_hardclock() - cycles;
501
502 /* Allow variation up to 20% */
503 if( cycles / millisecs < ratio - ratio / 5 ||
504 cycles / millisecs > ratio + ratio / 5 )
505 {
506 hardfail++;
507 goto hard_test;
508 }
509 }
510
511 if( verbose != 0 )
512 mbedtls_printf( "passed\n" );
513
514hard_test_done:
515
516 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200518
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100519 return( 0 );
520}
521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522#endif /* MBEDTLS_SELF_TEST */
Andrzej Kurek77daaad2022-03-04 15:10:06 -0500523#endif /* !MBEDTLS_TIMING_ALT */
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200524#endif /* MBEDTLS_TIMING_C */