blob: eb41461320be9fd4305d8a8677966c120745edf0 [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010024#else
25#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#define mbedtls_printf printf
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010027#endif
28
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +020029#if defined(MBEDTLS_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +020033#if !defined(MBEDTLS_TIMING_ALT)
34
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010035#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040036 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
Ørjan Malde479d8de2020-05-20 09:32:39 +000037 !defined(__HAIKU__) && !defined(__midipix__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010038#error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in config.h"
39#endif
40
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020041#ifndef asm
42#define asm __asm
43#endif
44
Paul Bakkerfa6a6202013-10-28 18:48:30 +010045#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000046
47#include <windows.h>
irwire931d0e2018-06-23 18:55:14 +030048#include <process.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000049
50struct _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
63struct _hr_time
64{
65 struct timeval start;
66};
67
Paul Bakker9af723c2014-05-01 13:03:14 +020068#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020070#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +020071 ( defined(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__)
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020073#define HAVE_HARDCLOCK
Paul Bakkerbb0139c2012-10-31 09:53:08 +000074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +000076{
77 unsigned long tsc;
78 __asm rdtsc
79 __asm mov [tsc], eax
80 return( tsc );
81}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020082#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +020083 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
Paul Bakker5121ce52009-01-03 21:22:43 +000084
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +000085/* some versions of mingw-64 have 32-bit longs even on x84_64 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020086#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +000087 defined(__GNUC__) && ( defined(__i386__) || ( \
88 ( defined(__amd64__) || defined( __x86_64__) ) && __SIZEOF_LONG__ == 4 ) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +000089
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020090#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +000091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +000093{
Paul Bakkerca410102011-10-19 14:27:36 +000094 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +010095 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakkerca410102011-10-19 14:27:36 +000096 return( lo );
Paul Bakker5121ce52009-01-03 21:22:43 +000097}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020098#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +020099 __GNUC__ && __i386__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200101#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +0200102 defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000103
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200104#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000107{
108 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100109 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200110 return( lo | ( hi << 32 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000111}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200112#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200113 __GNUC__ && ( __amd64__ || __x86_64__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200115#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +0200116 defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000117
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200118#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000121{
122 unsigned long tbl, tbu0, tbu1;
123
124 do
125 {
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100126 asm volatile( "mftbu %0" : "=r" (tbu0) );
127 asm volatile( "mftb %0" : "=r" (tbl ) );
128 asm volatile( "mftbu %0" : "=r" (tbu1) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 }
130 while( tbu0 != tbu1 );
131
132 return( tbl );
133}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200134#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200135 __GNUC__ && ( __powerpc__ || __ppc__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200137#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000138 defined(__GNUC__) && defined(__sparc64__)
139
140#if defined(__OpenBSD__)
141#warning OpenBSD does not allow access to tick register using software version instead
Paul Bakker5121ce52009-01-03 21:22:43 +0000142#else
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200143#define HAVE_HARDCLOCK
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145unsigned long mbedtls_timing_hardclock( void )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000146{
147 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100148 asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) );
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000149 return( tick );
150}
Paul Bakker9af723c2014-05-01 13:03:14 +0200151#endif /* __OpenBSD__ */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200152#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200153 __GNUC__ && __sparc64__ */
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000154
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200155#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000156 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
157
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200158#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000161{
162 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100163 asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" );
164 asm volatile( "mov %%g1, %0" : "=r" (tick) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 return( tick );
166}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200167#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200168 __GNUC__ && __sparc__ && !__sparc64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000169
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200170#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000171 defined(__GNUC__) && defined(__alpha__)
172
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200173#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000176{
177 unsigned long cc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100178 asm volatile( "rpcc %0" : "=r" (cc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 return( cc & 0xFFFFFFFF );
180}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200181#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200182 __GNUC__ && __alpha__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200184#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000185 defined(__GNUC__) && defined(__ia64__)
186
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200187#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000190{
191 unsigned long itc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100192 asm volatile( "mov %0 = ar.itc" : "=r" (itc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 return( itc );
194}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200195#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200196 __GNUC__ && __ia64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000197
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200198#if !defined(HAVE_HARDCLOCK) && defined(_MSC_VER) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100199 !defined(EFIX64) && !defined(EFI32)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000200
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200201#define HAVE_HARDCLOCK
Paul Bakker2eee9022011-04-24 15:28:55 +0000202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203unsigned long mbedtls_timing_hardclock( void )
Paul Bakker2eee9022011-04-24 15:28:55 +0000204{
205 LARGE_INTEGER offset;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100206
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100207 QueryPerformanceCounter( &offset );
Paul Bakker2eee9022011-04-24 15:28:55 +0000208
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200209 return( (unsigned long)( offset.QuadPart ) );
Paul Bakker2eee9022011-04-24 15:28:55 +0000210}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200211#endif /* !HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
Paul Bakker2eee9022011-04-24 15:28:55 +0000212
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200213#if !defined(HAVE_HARDCLOCK)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000214
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200215#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
217static int hardclock_init = 0;
218static struct timeval tv_init;
219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000221{
222 struct timeval tv_cur;
223
224 if( hardclock_init == 0 )
225 {
226 gettimeofday( &tv_init, NULL );
227 hardclock_init = 1;
228 }
229
230 gettimeofday( &tv_cur, NULL );
231 return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
232 + ( tv_cur.tv_usec - tv_init.tv_usec ) );
233}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200234#endif /* !HAVE_HARDCLOCK */
Paul Bakker5121ce52009-01-03 21:22:43 +0000235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236volatile int mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000237
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100238#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000241{
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 struct _hr_time *t = (struct _hr_time *) val;
243
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 if( reset )
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200245 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 QueryPerformanceCounter( &t->start );
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200247 return( 0 );
248 }
249 else
250 {
251 unsigned long delta;
252 LARGE_INTEGER now, hfreq;
253 QueryPerformanceCounter( &now );
254 QueryPerformanceFrequency( &hfreq );
255 delta = (unsigned long)( ( now.QuadPart - t->start.QuadPart ) * 1000ul
256 / hfreq.QuadPart );
257 return( delta );
258 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000259}
260
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000261/* It's OK to use a global because alarm() is supposed to be global anyway */
262static DWORD alarmMs;
263
irwire931d0e2018-06-23 18:55:14 +0300264static void TimerProc( void *TimerContext )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100265{
irwire1b82ad2018-08-30 11:57:09 +0300266 (void) TimerContext;
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000267 Sleep( alarmMs );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_timing_alarmed = 1;
irwirda642d92018-08-31 15:14:54 +0300269 /* _endthread will be called implicitly on return
270 * That ensures execution of thread funcition's epilogue */
Paul Bakker5121ce52009-01-03 21:22:43 +0000271}
272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273void mbedtls_set_alarm( int seconds )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100274{
Manuel Pégourié-Gonnard54059622018-01-29 10:16:30 +0100275 if( seconds == 0 )
276 {
277 /* No need to create a thread for this simple case.
278 * Also, this shorcut is more reliable at least on MinGW32 */
279 mbedtls_timing_alarmed = 1;
280 return;
281 }
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_timing_alarmed = 0;
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000284 alarmMs = seconds * 1000;
irwire1b82ad2018-08-30 11:57:09 +0300285 (void) _beginthread( TimerProc, 0, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000286}
287
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100288#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000291{
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 struct _hr_time *t = (struct _hr_time *) val;
293
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 if( reset )
295 {
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200296 gettimeofday( &t->start, NULL );
Alfred Klompb308dd72014-07-14 22:32:21 +0200297 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 }
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200299 else
300 {
301 unsigned long delta;
302 struct timeval now;
303 gettimeofday( &now, NULL );
304 delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul
305 + ( now.tv_usec - t->start.tv_usec ) / 1000;
306 return( delta );
307 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000308}
309
310static void sighandler( int signum )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100311{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 mbedtls_timing_alarmed = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 signal( signum, sighandler );
314}
315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316void mbedtls_set_alarm( int seconds )
Paul Bakker5121ce52009-01-03 21:22:43 +0000317{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 signal( SIGALRM, sighandler );
320 alarm( seconds );
Gilles Peskinea0af95f2017-10-10 20:10:46 +0200321 if( seconds == 0 )
322 {
323 /* alarm(0) cancelled any previous pending alarm, but the
324 handler won't fire, so raise the flag straight away. */
325 mbedtls_timing_alarmed = 1;
326 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000327}
328
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100329#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200331/*
332 * Set delays to watch
333 */
334void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms )
335{
336 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
337
338 ctx->int_ms = int_ms;
339 ctx->fin_ms = fin_ms;
340
341 if( fin_ms != 0 )
342 (void) mbedtls_timing_get_timer( &ctx->timer, 1 );
343}
344
345/*
346 * Get number of delays expired
347 */
348int mbedtls_timing_get_delay( void *data )
349{
350 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
351 unsigned long elapsed_ms;
352
353 if( ctx->fin_ms == 0 )
354 return( -1 );
355
356 elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 );
357
358 if( elapsed_ms >= ctx->fin_ms )
359 return( 2 );
360
361 if( elapsed_ms >= ctx->int_ms )
362 return( 1 );
363
364 return( 0 );
365}
366
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200367#endif /* !MBEDTLS_TIMING_ALT */
368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100370
371/*
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200372 * Busy-waits for the given number of milliseconds.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 * Used for testing mbedtls_timing_hardclock.
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200374 */
375static void busy_msleep( unsigned long msec )
376{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200378 unsigned long i = 0; /* for busy-waiting */
379 volatile unsigned long j; /* to prevent optimisation */
380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 while( mbedtls_timing_get_timer( &hires, 0 ) < msec )
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200384 i++;
385
386 j = i;
387 (void) j;
388}
389
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200390#define FAIL do \
391 { \
392 if( verbose != 0 ) \
393 { \
394 mbedtls_printf( "failed at line %d\n", __LINE__ ); \
395 mbedtls_printf( " cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d a=%lu b=%lu\n", \
396 cycles, ratio, millisecs, secs, hardfail, \
397 (unsigned long) a, (unsigned long) b ); \
398 mbedtls_printf( " elapsed(hires)=%lu elapsed(ctx)=%lu status(ctx)=%d\n", \
399 mbedtls_timing_get_timer( &hires, 0 ), \
400 mbedtls_timing_get_timer( &ctx.timer, 0 ), \
401 mbedtls_timing_get_delay( &ctx ) ); \
402 } \
403 return( 1 ); \
404 } while( 0 )
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200405
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200406/*
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100407 * Checkup routine
Manuel Pégourié-Gonnard0f79bab2014-04-09 09:56:16 +0200408 *
409 * Warning: this is work in progress, some tests may not be reliable enough
410 * yet! False positives may happen.
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412int mbedtls_timing_self_test( int verbose )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100413{
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200414 unsigned long cycles = 0, ratio = 0;
415 unsigned long millisecs = 0, secs = 0;
416 int hardfail = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 struct mbedtls_timing_hr_time hires;
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200418 uint32_t a = 0, b = 0;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200419 mbedtls_timing_delay_context ctx;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100420
Paul Bakker66d5d072014-06-17 16:39:18 +0200421 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_printf( " TIMING tests note: will take some time!\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100423
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100424 if( verbose != 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200425 mbedtls_printf( " TIMING test #1 (set_alarm / get_timer): " );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100426
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100427 {
Gilles Peskineada3ee82017-12-20 22:31:17 +0100428 secs = 1;
429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_set_alarm( (int) secs );
433 while( !mbedtls_timing_alarmed )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100434 ;
435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 millisecs = mbedtls_timing_get_timer( &hires, 0 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100437
Manuel Pégourié-Gonnarde578b1c2015-08-18 20:11:48 +0200438 /* For some reason on Windows it looks like alarm has an extra delay
439 * (maybe related to creating a new thread). Allow some room here. */
440 if( millisecs < 800 * secs || millisecs > 1200 * secs + 300 )
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200441 FAIL;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100442 }
443
444 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100446
447 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200448 mbedtls_printf( " TIMING test #2 (set/get_delay ): " );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200449
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200450 {
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200451 a = 800;
452 b = 400;
453 mbedtls_timing_set_delay( &ctx, a, a + b ); /* T = 0 */
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200454
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200455 busy_msleep( a - a / 4 ); /* T = a - a/4 */
456 if( mbedtls_timing_get_delay( &ctx ) != 0 )
457 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200458
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200459 busy_msleep( a / 4 + b / 4 ); /* T = a + b/4 */
460 if( mbedtls_timing_get_delay( &ctx ) != 1 )
461 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200462
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200463 busy_msleep( b ); /* T = a + b + b/4 */
464 if( mbedtls_timing_get_delay( &ctx ) != 2 )
465 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200466 }
467
468 mbedtls_timing_set_delay( &ctx, 0, 0 );
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200469 busy_msleep( 200 );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200470 if( mbedtls_timing_get_delay( &ctx ) != -1 )
471 FAIL;
472
473 if( verbose != 0 )
474 mbedtls_printf( "passed\n" );
475
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200476 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200477 mbedtls_printf( " TIMING test #3 (hardclock / get_timer): " );
478
479 /*
480 * Allow one failure for possible counter wrapping.
481 * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
482 * since the whole test is about 10ms, it shouldn't happen twice in a row.
483 */
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200484
485hard_test:
486 if( hardfail > 1 )
487 {
488 if( verbose != 0 )
489 mbedtls_printf( "failed (ignored)\n" );
490
491 goto hard_test_done;
492 }
493
494 /* Get a reference ratio cycles/ms */
495 millisecs = 1;
496 cycles = mbedtls_timing_hardclock();
497 busy_msleep( millisecs );
498 cycles = mbedtls_timing_hardclock() - cycles;
499 ratio = cycles / millisecs;
500
501 /* Check that the ratio is mostly constant */
502 for( millisecs = 2; millisecs <= 4; millisecs++ )
503 {
504 cycles = mbedtls_timing_hardclock();
505 busy_msleep( millisecs );
506 cycles = mbedtls_timing_hardclock() - cycles;
507
508 /* Allow variation up to 20% */
509 if( cycles / millisecs < ratio - ratio / 5 ||
510 cycles / millisecs > ratio + ratio / 5 )
511 {
512 hardfail++;
513 goto hard_test;
514 }
515 }
516
517 if( verbose != 0 )
518 mbedtls_printf( "passed\n" );
519
520hard_test_done:
521
522 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200524
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100525 return( 0 );
526}
527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100529
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200530#endif /* MBEDTLS_TIMING_C */