blob: 3e8139f1f9a1c67df1b5c051edab8b5a61739369 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Portable interface to the CPU cycle counter
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010030#else
31#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define mbedtls_printf printf
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010033#endif
34
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +020035#if defined(MBEDTLS_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +020039#if !defined(MBEDTLS_TIMING_ALT)
40
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010041#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040042 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
43 !defined(__HAIKU__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010044#error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in config.h"
45#endif
46
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020047#ifndef asm
48#define asm __asm
49#endif
50
Paul Bakkerfa6a6202013-10-28 18:48:30 +010051#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000052
53#include <windows.h>
54#include <winbase.h>
55
56struct _hr_time
57{
58 LARGE_INTEGER start;
59};
60
61#else
62
63#include <unistd.h>
64#include <sys/types.h>
65#include <sys/time.h>
66#include <signal.h>
67#include <time.h>
68
69struct _hr_time
70{
71 struct timeval start;
72};
73
Paul Bakker9af723c2014-05-01 13:03:14 +020074#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000075
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020076#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +020077 ( defined(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__)
Paul Bakker5121ce52009-01-03 21:22:43 +000078
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020079#define HAVE_HARDCLOCK
Paul Bakkerbb0139c2012-10-31 09:53:08 +000080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +000082{
83 unsigned long tsc;
84 __asm rdtsc
85 __asm mov [tsc], eax
86 return( tsc );
87}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020088#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +020089 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
Paul Bakker5121ce52009-01-03 21:22:43 +000090
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +000091/* some versions of mingw-64 have 32-bit longs even on x84_64 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020092#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +000093 defined(__GNUC__) && ( defined(__i386__) || ( \
94 ( defined(__amd64__) || defined( __x86_64__) ) && __SIZEOF_LONG__ == 4 ) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +000095
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020096#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +000097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +000099{
Paul Bakkerca410102011-10-19 14:27:36 +0000100 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100101 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakkerca410102011-10-19 14:27:36 +0000102 return( lo );
Paul Bakker5121ce52009-01-03 21:22:43 +0000103}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200104#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200105 __GNUC__ && __i386__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200107#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +0200108 defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000109
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200110#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000113{
114 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100115 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200116 return( lo | ( hi << 32 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000117}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200118#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200119 __GNUC__ && ( __amd64__ || __x86_64__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200121#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +0200122 defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000123
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200124#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000127{
128 unsigned long tbl, tbu0, tbu1;
129
130 do
131 {
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100132 asm volatile( "mftbu %0" : "=r" (tbu0) );
133 asm volatile( "mftb %0" : "=r" (tbl ) );
134 asm volatile( "mftbu %0" : "=r" (tbu1) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 }
136 while( tbu0 != tbu1 );
137
138 return( tbl );
139}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200140#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200141 __GNUC__ && ( __powerpc__ || __ppc__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200143#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000144 defined(__GNUC__) && defined(__sparc64__)
145
146#if defined(__OpenBSD__)
147#warning OpenBSD does not allow access to tick register using software version instead
Paul Bakker5121ce52009-01-03 21:22:43 +0000148#else
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200149#define HAVE_HARDCLOCK
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151unsigned long mbedtls_timing_hardclock( void )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000152{
153 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100154 asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) );
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000155 return( tick );
156}
Paul Bakker9af723c2014-05-01 13:03:14 +0200157#endif /* __OpenBSD__ */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200158#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200159 __GNUC__ && __sparc64__ */
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000160
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200161#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000162 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
163
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200164#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000167{
168 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100169 asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" );
170 asm volatile( "mov %%g1, %0" : "=r" (tick) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000171 return( tick );
172}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200173#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200174 __GNUC__ && __sparc__ && !__sparc64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000175
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200176#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000177 defined(__GNUC__) && defined(__alpha__)
178
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200179#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000182{
183 unsigned long cc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100184 asm volatile( "rpcc %0" : "=r" (cc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 return( cc & 0xFFFFFFFF );
186}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200187#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200188 __GNUC__ && __alpha__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200190#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000191 defined(__GNUC__) && defined(__ia64__)
192
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200193#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000196{
197 unsigned long itc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100198 asm volatile( "mov %0 = ar.itc" : "=r" (itc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 return( itc );
200}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200201#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200202 __GNUC__ && __ia64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000203
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200204#if !defined(HAVE_HARDCLOCK) && defined(_MSC_VER) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100205 !defined(EFIX64) && !defined(EFI32)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000206
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200207#define HAVE_HARDCLOCK
Paul Bakker2eee9022011-04-24 15:28:55 +0000208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209unsigned long mbedtls_timing_hardclock( void )
Paul Bakker2eee9022011-04-24 15:28:55 +0000210{
211 LARGE_INTEGER offset;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100212
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100213 QueryPerformanceCounter( &offset );
Paul Bakker2eee9022011-04-24 15:28:55 +0000214
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200215 return( (unsigned long)( offset.QuadPart ) );
Paul Bakker2eee9022011-04-24 15:28:55 +0000216}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200217#endif /* !HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
Paul Bakker2eee9022011-04-24 15:28:55 +0000218
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200219#if !defined(HAVE_HARDCLOCK)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000220
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200221#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000222
223static int hardclock_init = 0;
224static struct timeval tv_init;
225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000227{
228 struct timeval tv_cur;
229
230 if( hardclock_init == 0 )
231 {
232 gettimeofday( &tv_init, NULL );
233 hardclock_init = 1;
234 }
235
236 gettimeofday( &tv_cur, NULL );
237 return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
238 + ( tv_cur.tv_usec - tv_init.tv_usec ) );
239}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200240#endif /* !HAVE_HARDCLOCK */
Paul Bakker5121ce52009-01-03 21:22:43 +0000241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242volatile int mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000243
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100244#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000247{
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 struct _hr_time *t = (struct _hr_time *) val;
249
Paul Bakker5121ce52009-01-03 21:22:43 +0000250 if( reset )
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200251 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 QueryPerformanceCounter( &t->start );
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200253 return( 0 );
254 }
255 else
256 {
257 unsigned long delta;
258 LARGE_INTEGER now, hfreq;
259 QueryPerformanceCounter( &now );
260 QueryPerformanceFrequency( &hfreq );
261 delta = (unsigned long)( ( now.QuadPart - t->start.QuadPart ) * 1000ul
262 / hfreq.QuadPart );
263 return( delta );
264 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000265}
266
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000267/* It's OK to use a global because alarm() is supposed to be global anyway */
268static DWORD alarmMs;
269
Manuel Pégourié-Gonnard6d71e4e2015-02-11 12:54:35 +0000270static DWORD WINAPI TimerProc( LPVOID TimerContext )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100271{
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000272 ((void) TimerContext);
273 Sleep( alarmMs );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 mbedtls_timing_alarmed = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 return( TRUE );
276}
277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278void mbedtls_set_alarm( int seconds )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100279{
Paul Bakker5121ce52009-01-03 21:22:43 +0000280 DWORD ThreadId;
281
Manuel Pégourié-Gonnard54059622018-01-29 10:16:30 +0100282 if( seconds == 0 )
283 {
284 /* No need to create a thread for this simple case.
285 * Also, this shorcut is more reliable at least on MinGW32 */
286 mbedtls_timing_alarmed = 1;
287 return;
288 }
289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 mbedtls_timing_alarmed = 0;
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000291 alarmMs = seconds * 1000;
292 CloseHandle( CreateThread( NULL, 0, TimerProc, NULL, 0, &ThreadId ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000293}
294
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100295#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000298{
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 struct _hr_time *t = (struct _hr_time *) val;
300
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 if( reset )
302 {
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200303 gettimeofday( &t->start, NULL );
Alfred Klompb308dd72014-07-14 22:32:21 +0200304 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000305 }
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200306 else
307 {
308 unsigned long delta;
309 struct timeval now;
310 gettimeofday( &now, NULL );
311 delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul
312 + ( now.tv_usec - t->start.tv_usec ) / 1000;
313 return( delta );
314 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000315}
316
317static void sighandler( int signum )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100318{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_timing_alarmed = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000320 signal( signum, sighandler );
321}
322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323void mbedtls_set_alarm( int seconds )
Paul Bakker5121ce52009-01-03 21:22:43 +0000324{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 signal( SIGALRM, sighandler );
327 alarm( seconds );
Gilles Peskinea0af95f2017-10-10 20:10:46 +0200328 if( seconds == 0 )
329 {
330 /* alarm(0) cancelled any previous pending alarm, but the
331 handler won't fire, so raise the flag straight away. */
332 mbedtls_timing_alarmed = 1;
333 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000334}
335
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100336#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000337
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200338/*
339 * Set delays to watch
340 */
341void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms )
342{
343 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
344
345 ctx->int_ms = int_ms;
346 ctx->fin_ms = fin_ms;
347
348 if( fin_ms != 0 )
349 (void) mbedtls_timing_get_timer( &ctx->timer, 1 );
350}
351
352/*
353 * Get number of delays expired
354 */
355int mbedtls_timing_get_delay( void *data )
356{
357 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
358 unsigned long elapsed_ms;
359
360 if( ctx->fin_ms == 0 )
361 return( -1 );
362
363 elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 );
364
365 if( elapsed_ms >= ctx->fin_ms )
366 return( 2 );
367
368 if( elapsed_ms >= ctx->int_ms )
369 return( 1 );
370
371 return( 0 );
372}
373
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200374#endif /* !MBEDTLS_TIMING_ALT */
375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100377
378/*
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200379 * Busy-waits for the given number of milliseconds.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 * Used for testing mbedtls_timing_hardclock.
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200381 */
382static void busy_msleep( unsigned long msec )
383{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200385 unsigned long i = 0; /* for busy-waiting */
386 volatile unsigned long j; /* to prevent optimisation */
387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 while( mbedtls_timing_get_timer( &hires, 0 ) < msec )
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200391 i++;
392
393 j = i;
394 (void) j;
395}
396
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200397#define FAIL do \
398 { \
399 if( verbose != 0 ) \
400 { \
401 mbedtls_printf( "failed at line %d\n", __LINE__ ); \
402 mbedtls_printf( " cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d a=%lu b=%lu\n", \
403 cycles, ratio, millisecs, secs, hardfail, \
404 (unsigned long) a, (unsigned long) b ); \
405 mbedtls_printf( " elapsed(hires)=%lu elapsed(ctx)=%lu status(ctx)=%d\n", \
406 mbedtls_timing_get_timer( &hires, 0 ), \
407 mbedtls_timing_get_timer( &ctx.timer, 0 ), \
408 mbedtls_timing_get_delay( &ctx ) ); \
409 } \
410 return( 1 ); \
411 } while( 0 )
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200412
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200413/*
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100414 * Checkup routine
Manuel Pégourié-Gonnard0f79bab2014-04-09 09:56:16 +0200415 *
416 * Warning: this is work in progress, some tests may not be reliable enough
417 * yet! False positives may happen.
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419int mbedtls_timing_self_test( int verbose )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100420{
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200421 unsigned long cycles = 0, ratio = 0;
422 unsigned long millisecs = 0, secs = 0;
423 int hardfail = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 struct mbedtls_timing_hr_time hires;
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200425 uint32_t a = 0, b = 0;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200426 mbedtls_timing_delay_context ctx;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100427
Paul Bakker66d5d072014-06-17 16:39:18 +0200428 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_printf( " TIMING tests note: will take some time!\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100430
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100431 if( verbose != 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200432 mbedtls_printf( " TIMING test #1 (set_alarm / get_timer): " );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100433
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100434 {
Gilles Peskineada3ee82017-12-20 22:31:17 +0100435 secs = 1;
436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_set_alarm( (int) secs );
440 while( !mbedtls_timing_alarmed )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100441 ;
442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 millisecs = mbedtls_timing_get_timer( &hires, 0 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100444
Manuel Pégourié-Gonnarde578b1c2015-08-18 20:11:48 +0200445 /* For some reason on Windows it looks like alarm has an extra delay
446 * (maybe related to creating a new thread). Allow some room here. */
447 if( millisecs < 800 * secs || millisecs > 1200 * secs + 300 )
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200448 FAIL;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100449 }
450
451 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100453
454 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200455 mbedtls_printf( " TIMING test #2 (set/get_delay ): " );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200456
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200457 {
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200458 a = 800;
459 b = 400;
460 mbedtls_timing_set_delay( &ctx, a, a + b ); /* T = 0 */
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200461
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200462 busy_msleep( a - a / 4 ); /* T = a - a/4 */
463 if( mbedtls_timing_get_delay( &ctx ) != 0 )
464 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200465
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200466 busy_msleep( a / 4 + b / 4 ); /* T = a + b/4 */
467 if( mbedtls_timing_get_delay( &ctx ) != 1 )
468 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200469
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200470 busy_msleep( b ); /* T = a + b + b/4 */
471 if( mbedtls_timing_get_delay( &ctx ) != 2 )
472 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200473 }
474
475 mbedtls_timing_set_delay( &ctx, 0, 0 );
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200476 busy_msleep( 200 );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200477 if( mbedtls_timing_get_delay( &ctx ) != -1 )
478 FAIL;
479
480 if( verbose != 0 )
481 mbedtls_printf( "passed\n" );
482
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200483 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200484 mbedtls_printf( " TIMING test #3 (hardclock / get_timer): " );
485
486 /*
487 * Allow one failure for possible counter wrapping.
488 * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
489 * since the whole test is about 10ms, it shouldn't happen twice in a row.
490 */
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200491
492hard_test:
493 if( hardfail > 1 )
494 {
495 if( verbose != 0 )
496 mbedtls_printf( "failed (ignored)\n" );
497
498 goto hard_test_done;
499 }
500
501 /* Get a reference ratio cycles/ms */
502 millisecs = 1;
503 cycles = mbedtls_timing_hardclock();
504 busy_msleep( millisecs );
505 cycles = mbedtls_timing_hardclock() - cycles;
506 ratio = cycles / millisecs;
507
508 /* Check that the ratio is mostly constant */
509 for( millisecs = 2; millisecs <= 4; millisecs++ )
510 {
511 cycles = mbedtls_timing_hardclock();
512 busy_msleep( millisecs );
513 cycles = mbedtls_timing_hardclock() - cycles;
514
515 /* Allow variation up to 20% */
516 if( cycles / millisecs < ratio - ratio / 5 ||
517 cycles / millisecs > ratio + ratio / 5 )
518 {
519 hardfail++;
520 goto hard_test;
521 }
522 }
523
524 if( verbose != 0 )
525 mbedtls_printf( "passed\n" );
526
527hard_test_done:
528
529 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200531
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100532 return( 0 );
533}
534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100536
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200537#endif /* MBEDTLS_TIMING_C */