blob: 50a22165a6c48a4d5e8db45af72758b71f956c4f [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Portable interface to the CPU cycle counter
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010055#else
56#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define mbedtls_printf printf
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010058#endif
59
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +020060#if defined(MBEDTLS_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000063
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +020064#if !defined(MBEDTLS_TIMING_ALT)
65
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010066#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040067 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
68 !defined(__HAIKU__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010069#error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in config.h"
70#endif
71
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020072#ifndef asm
73#define asm __asm
74#endif
75
Paul Bakkerfa6a6202013-10-28 18:48:30 +010076#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000077
78#include <windows.h>
irwire931d0e2018-06-23 18:55:14 +030079#include <process.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000080
81struct _hr_time
82{
83 LARGE_INTEGER start;
84};
85
86#else
87
88#include <unistd.h>
89#include <sys/types.h>
90#include <sys/time.h>
91#include <signal.h>
92#include <time.h>
93
94struct _hr_time
95{
96 struct timeval start;
97};
98
Paul Bakker9af723c2014-05-01 13:03:14 +020099#endif /* _WIN32 && !EFIX64 && !EFI32 */
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(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__)
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200104#define HAVE_HARDCLOCK
Paul Bakkerbb0139c2012-10-31 09:53:08 +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 tsc;
109 __asm rdtsc
110 __asm mov [tsc], eax
111 return( tsc );
112}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200113#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200114 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +0000116/* some versions of mingw-64 have 32-bit longs even on x84_64 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200117#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +0000118 defined(__GNUC__) && ( defined(__i386__) || ( \
119 ( defined(__amd64__) || defined( __x86_64__) ) && __SIZEOF_LONG__ == 4 ) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000120
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200121#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000124{
Paul Bakkerca410102011-10-19 14:27:36 +0000125 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100126 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakkerca410102011-10-19 14:27:36 +0000127 return( lo );
Paul Bakker5121ce52009-01-03 21:22:43 +0000128}
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__ && __i386__ */
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 Bakker66d5d072014-06-17 16:39:18 +0200133 defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000134
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200135#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000138{
139 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100140 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200141 return( lo | ( hi << 32 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200143#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200144 __GNUC__ && ( __amd64__ || __x86_64__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200146#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +0200147 defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000148
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200149#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000152{
153 unsigned long tbl, tbu0, tbu1;
154
155 do
156 {
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100157 asm volatile( "mftbu %0" : "=r" (tbu0) );
158 asm volatile( "mftb %0" : "=r" (tbl ) );
159 asm volatile( "mftbu %0" : "=r" (tbu1) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 }
161 while( tbu0 != tbu1 );
162
163 return( tbl );
164}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200165#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200166 __GNUC__ && ( __powerpc__ || __ppc__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200168#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000169 defined(__GNUC__) && defined(__sparc64__)
170
171#if defined(__OpenBSD__)
172#warning OpenBSD does not allow access to tick register using software version instead
Paul Bakker5121ce52009-01-03 21:22:43 +0000173#else
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200174#define HAVE_HARDCLOCK
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176unsigned long mbedtls_timing_hardclock( void )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000177{
178 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100179 asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) );
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000180 return( tick );
181}
Paul Bakker9af723c2014-05-01 13:03:14 +0200182#endif /* __OpenBSD__ */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200183#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200184 __GNUC__ && __sparc64__ */
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000185
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200186#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000187 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
188
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200189#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000192{
193 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100194 asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" );
195 asm volatile( "mov %%g1, %0" : "=r" (tick) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 return( tick );
197}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200198#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200199 __GNUC__ && __sparc__ && !__sparc64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200201#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000202 defined(__GNUC__) && defined(__alpha__)
203
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200204#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000207{
208 unsigned long cc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100209 asm volatile( "rpcc %0" : "=r" (cc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 return( cc & 0xFFFFFFFF );
211}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200212#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200213 __GNUC__ && __alpha__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200215#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000216 defined(__GNUC__) && defined(__ia64__)
217
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200218#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000221{
222 unsigned long itc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100223 asm volatile( "mov %0 = ar.itc" : "=r" (itc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 return( itc );
225}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200226#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200227 __GNUC__ && __ia64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200229#if !defined(HAVE_HARDCLOCK) && defined(_MSC_VER) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100230 !defined(EFIX64) && !defined(EFI32)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000231
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200232#define HAVE_HARDCLOCK
Paul Bakker2eee9022011-04-24 15:28:55 +0000233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234unsigned long mbedtls_timing_hardclock( void )
Paul Bakker2eee9022011-04-24 15:28:55 +0000235{
236 LARGE_INTEGER offset;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100237
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100238 QueryPerformanceCounter( &offset );
Paul Bakker2eee9022011-04-24 15:28:55 +0000239
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200240 return( (unsigned long)( offset.QuadPart ) );
Paul Bakker2eee9022011-04-24 15:28:55 +0000241}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200242#endif /* !HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
Paul Bakker2eee9022011-04-24 15:28:55 +0000243
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200244#if !defined(HAVE_HARDCLOCK)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000245
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200246#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000247
248static int hardclock_init = 0;
249static struct timeval tv_init;
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000252{
253 struct timeval tv_cur;
254
255 if( hardclock_init == 0 )
256 {
257 gettimeofday( &tv_init, NULL );
258 hardclock_init = 1;
259 }
260
261 gettimeofday( &tv_cur, NULL );
262 return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
263 + ( tv_cur.tv_usec - tv_init.tv_usec ) );
264}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200265#endif /* !HAVE_HARDCLOCK */
Paul Bakker5121ce52009-01-03 21:22:43 +0000266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267volatile int mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000268
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100269#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000272{
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 struct _hr_time *t = (struct _hr_time *) val;
274
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 if( reset )
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200276 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 QueryPerformanceCounter( &t->start );
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200278 return( 0 );
279 }
280 else
281 {
282 unsigned long delta;
283 LARGE_INTEGER now, hfreq;
284 QueryPerformanceCounter( &now );
285 QueryPerformanceFrequency( &hfreq );
286 delta = (unsigned long)( ( now.QuadPart - t->start.QuadPart ) * 1000ul
287 / hfreq.QuadPart );
288 return( delta );
289 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000290}
291
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000292/* It's OK to use a global because alarm() is supposed to be global anyway */
293static DWORD alarmMs;
294
irwire931d0e2018-06-23 18:55:14 +0300295static void TimerProc( void *TimerContext )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100296{
irwire1b82ad2018-08-30 11:57:09 +0300297 (void) TimerContext;
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000298 Sleep( alarmMs );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 mbedtls_timing_alarmed = 1;
irwirda642d92018-08-31 15:14:54 +0300300 /* _endthread will be called implicitly on return
301 * That ensures execution of thread funcition's epilogue */
Paul Bakker5121ce52009-01-03 21:22:43 +0000302}
303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304void mbedtls_set_alarm( int seconds )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100305{
Manuel Pégourié-Gonnard54059622018-01-29 10:16:30 +0100306 if( seconds == 0 )
307 {
308 /* No need to create a thread for this simple case.
309 * Also, this shorcut is more reliable at least on MinGW32 */
310 mbedtls_timing_alarmed = 1;
311 return;
312 }
313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 mbedtls_timing_alarmed = 0;
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000315 alarmMs = seconds * 1000;
irwire1b82ad2018-08-30 11:57:09 +0300316 (void) _beginthread( TimerProc, 0, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000317}
318
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100319#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000322{
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 struct _hr_time *t = (struct _hr_time *) val;
324
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 if( reset )
326 {
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200327 gettimeofday( &t->start, NULL );
Alfred Klompb308dd72014-07-14 22:32:21 +0200328 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 }
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200330 else
331 {
332 unsigned long delta;
333 struct timeval now;
334 gettimeofday( &now, NULL );
335 delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul
336 + ( now.tv_usec - t->start.tv_usec ) / 1000;
337 return( delta );
338 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000339}
340
341static void sighandler( int signum )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100342{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 mbedtls_timing_alarmed = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 signal( signum, sighandler );
345}
346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347void mbedtls_set_alarm( int seconds )
Paul Bakker5121ce52009-01-03 21:22:43 +0000348{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 signal( SIGALRM, sighandler );
351 alarm( seconds );
Gilles Peskinea0af95f2017-10-10 20:10:46 +0200352 if( seconds == 0 )
353 {
354 /* alarm(0) cancelled any previous pending alarm, but the
355 handler won't fire, so raise the flag straight away. */
356 mbedtls_timing_alarmed = 1;
357 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000358}
359
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100360#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000361
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200362/*
363 * Set delays to watch
364 */
365void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms )
366{
367 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
368
369 ctx->int_ms = int_ms;
370 ctx->fin_ms = fin_ms;
371
372 if( fin_ms != 0 )
373 (void) mbedtls_timing_get_timer( &ctx->timer, 1 );
374}
375
376/*
377 * Get number of delays expired
378 */
379int mbedtls_timing_get_delay( void *data )
380{
381 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
382 unsigned long elapsed_ms;
383
384 if( ctx->fin_ms == 0 )
385 return( -1 );
386
387 elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 );
388
389 if( elapsed_ms >= ctx->fin_ms )
390 return( 2 );
391
392 if( elapsed_ms >= ctx->int_ms )
393 return( 1 );
394
395 return( 0 );
396}
397
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200398#endif /* !MBEDTLS_TIMING_ALT */
399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100401
402/*
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200403 * Busy-waits for the given number of milliseconds.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 * Used for testing mbedtls_timing_hardclock.
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200405 */
406static void busy_msleep( unsigned long msec )
407{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200409 unsigned long i = 0; /* for busy-waiting */
410 volatile unsigned long j; /* to prevent optimisation */
411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 while( mbedtls_timing_get_timer( &hires, 0 ) < msec )
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200415 i++;
416
417 j = i;
418 (void) j;
419}
420
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200421#define FAIL do \
422 { \
423 if( verbose != 0 ) \
424 { \
425 mbedtls_printf( "failed at line %d\n", __LINE__ ); \
426 mbedtls_printf( " cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d a=%lu b=%lu\n", \
427 cycles, ratio, millisecs, secs, hardfail, \
428 (unsigned long) a, (unsigned long) b ); \
429 mbedtls_printf( " elapsed(hires)=%lu elapsed(ctx)=%lu status(ctx)=%d\n", \
430 mbedtls_timing_get_timer( &hires, 0 ), \
431 mbedtls_timing_get_timer( &ctx.timer, 0 ), \
432 mbedtls_timing_get_delay( &ctx ) ); \
433 } \
434 return( 1 ); \
435 } while( 0 )
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200436
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200437/*
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100438 * Checkup routine
Manuel Pégourié-Gonnard0f79bab2014-04-09 09:56:16 +0200439 *
440 * Warning: this is work in progress, some tests may not be reliable enough
441 * yet! False positives may happen.
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443int mbedtls_timing_self_test( int verbose )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100444{
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200445 unsigned long cycles = 0, ratio = 0;
446 unsigned long millisecs = 0, secs = 0;
447 int hardfail = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 struct mbedtls_timing_hr_time hires;
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200449 uint32_t a = 0, b = 0;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200450 mbedtls_timing_delay_context ctx;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100451
Paul Bakker66d5d072014-06-17 16:39:18 +0200452 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_printf( " TIMING tests note: will take some time!\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100454
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100455 if( verbose != 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200456 mbedtls_printf( " TIMING test #1 (set_alarm / get_timer): " );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100457
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100458 {
Gilles Peskineada3ee82017-12-20 22:31:17 +0100459 secs = 1;
460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_set_alarm( (int) secs );
464 while( !mbedtls_timing_alarmed )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100465 ;
466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 millisecs = mbedtls_timing_get_timer( &hires, 0 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100468
Manuel Pégourié-Gonnarde578b1c2015-08-18 20:11:48 +0200469 /* For some reason on Windows it looks like alarm has an extra delay
470 * (maybe related to creating a new thread). Allow some room here. */
471 if( millisecs < 800 * secs || millisecs > 1200 * secs + 300 )
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200472 FAIL;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100473 }
474
475 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100477
478 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200479 mbedtls_printf( " TIMING test #2 (set/get_delay ): " );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200480
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200481 {
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200482 a = 800;
483 b = 400;
484 mbedtls_timing_set_delay( &ctx, a, a + b ); /* T = 0 */
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200485
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200486 busy_msleep( a - a / 4 ); /* T = a - a/4 */
487 if( mbedtls_timing_get_delay( &ctx ) != 0 )
488 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200489
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200490 busy_msleep( a / 4 + b / 4 ); /* T = a + b/4 */
491 if( mbedtls_timing_get_delay( &ctx ) != 1 )
492 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200493
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200494 busy_msleep( b ); /* T = a + b + b/4 */
495 if( mbedtls_timing_get_delay( &ctx ) != 2 )
496 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200497 }
498
499 mbedtls_timing_set_delay( &ctx, 0, 0 );
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200500 busy_msleep( 200 );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200501 if( mbedtls_timing_get_delay( &ctx ) != -1 )
502 FAIL;
503
504 if( verbose != 0 )
505 mbedtls_printf( "passed\n" );
506
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200507 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200508 mbedtls_printf( " TIMING test #3 (hardclock / get_timer): " );
509
510 /*
511 * Allow one failure for possible counter wrapping.
512 * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
513 * since the whole test is about 10ms, it shouldn't happen twice in a row.
514 */
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200515
516hard_test:
517 if( hardfail > 1 )
518 {
519 if( verbose != 0 )
520 mbedtls_printf( "failed (ignored)\n" );
521
522 goto hard_test_done;
523 }
524
525 /* Get a reference ratio cycles/ms */
526 millisecs = 1;
527 cycles = mbedtls_timing_hardclock();
528 busy_msleep( millisecs );
529 cycles = mbedtls_timing_hardclock() - cycles;
530 ratio = cycles / millisecs;
531
532 /* Check that the ratio is mostly constant */
533 for( millisecs = 2; millisecs <= 4; millisecs++ )
534 {
535 cycles = mbedtls_timing_hardclock();
536 busy_msleep( millisecs );
537 cycles = mbedtls_timing_hardclock() - cycles;
538
539 /* Allow variation up to 20% */
540 if( cycles / millisecs < ratio - ratio / 5 ||
541 cycles / millisecs > ratio + ratio / 5 )
542 {
543 hardfail++;
544 goto hard_test;
545 }
546 }
547
548 if( verbose != 0 )
549 mbedtls_printf( "passed\n" );
550
551hard_test_done:
552
553 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200555
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100556 return( 0 );
557}
558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100560
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200561#endif /* MBEDTLS_TIMING_C */