blob: de936e101ed8e56a86786dd77111577832cb4c87 [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é-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>
48#include <winbase.h>
49
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 Peskineb29e70b2017-10-16 19:33:06 +0200245 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 QueryPerformanceCounter( &t->start );
Gilles Peskineb29e70b2017-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
Manuel Pégourié-Gonnard6d71e4e2015-02-11 12:54:35 +0000264static DWORD WINAPI TimerProc( LPVOID TimerContext )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100265{
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000266 ((void) TimerContext);
267 Sleep( alarmMs );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_timing_alarmed = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 return( TRUE );
270}
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272void mbedtls_set_alarm( int seconds )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100273{
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 DWORD ThreadId;
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_timing_alarmed = 0;
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000277 alarmMs = seconds * 1000;
278 CloseHandle( CreateThread( NULL, 0, TimerProc, NULL, 0, &ThreadId ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000279}
280
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100281#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000284{
Paul Bakker5121ce52009-01-03 21:22:43 +0000285 struct _hr_time *t = (struct _hr_time *) val;
286
Paul Bakker5121ce52009-01-03 21:22:43 +0000287 if( reset )
288 {
Gilles Peskineb29e70b2017-10-16 19:33:06 +0200289 gettimeofday( &t->start, NULL );
Alfred Klompb308dd72014-07-14 22:32:21 +0200290 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 }
Gilles Peskineb29e70b2017-10-16 19:33:06 +0200292 else
293 {
294 unsigned long delta;
295 struct timeval now;
296 gettimeofday( &now, NULL );
297 delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul
298 + ( now.tv_usec - t->start.tv_usec ) / 1000;
299 return( delta );
300 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000301}
302
303static void sighandler( int signum )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100304{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 mbedtls_timing_alarmed = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 signal( signum, sighandler );
307}
308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309void mbedtls_set_alarm( int seconds )
Paul Bakker5121ce52009-01-03 21:22:43 +0000310{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 signal( SIGALRM, sighandler );
313 alarm( seconds );
Gilles Peskine3099b432017-10-10 20:10:46 +0200314 if( seconds == 0 )
315 {
316 /* alarm(0) cancelled any previous pending alarm, but the
317 handler won't fire, so raise the flag straight away. */
318 mbedtls_timing_alarmed = 1;
319 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000320}
321
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100322#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000323
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200324/*
325 * Set delays to watch
326 */
327void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms )
328{
329 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
330
331 ctx->int_ms = int_ms;
332 ctx->fin_ms = fin_ms;
333
334 if( fin_ms != 0 )
335 (void) mbedtls_timing_get_timer( &ctx->timer, 1 );
336}
337
338/*
339 * Get number of delays expired
340 */
341int mbedtls_timing_get_delay( void *data )
342{
343 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
344 unsigned long elapsed_ms;
345
346 if( ctx->fin_ms == 0 )
347 return( -1 );
348
349 elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 );
350
351 if( elapsed_ms >= ctx->fin_ms )
352 return( 2 );
353
354 if( elapsed_ms >= ctx->int_ms )
355 return( 1 );
356
357 return( 0 );
358}
359
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200360#endif /* !MBEDTLS_TIMING_ALT */
361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100363
364/*
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200365 * Busy-waits for the given number of milliseconds.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 * Used for testing mbedtls_timing_hardclock.
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200367 */
368static void busy_msleep( unsigned long msec )
369{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200371 unsigned long i = 0; /* for busy-waiting */
372 volatile unsigned long j; /* to prevent optimisation */
373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 while( mbedtls_timing_get_timer( &hires, 0 ) < msec )
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200377 i++;
378
379 j = i;
380 (void) j;
381}
382
Gilles Peskine105e6bc2017-10-10 20:09:26 +0200383#define FAIL do \
384 { \
385 if( verbose != 0 ) \
386 { \
387 mbedtls_printf( "failed at line %d\n", __LINE__ ); \
388 mbedtls_printf( " cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d a=%lu b=%lu\n", \
389 cycles, ratio, millisecs, secs, hardfail, \
390 (unsigned long) a, (unsigned long) b ); \
391 mbedtls_printf( " elapsed(hires)=%lu elapsed(ctx)=%lu status(ctx)=%d\n", \
392 mbedtls_timing_get_timer( &hires, 0 ), \
393 mbedtls_timing_get_timer( &ctx.timer, 0 ), \
394 mbedtls_timing_get_delay( &ctx ) ); \
395 } \
396 return( 1 ); \
397 } while( 0 )
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200398
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200399/*
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100400 * Checkup routine
Manuel Pégourié-Gonnard0f79bab2014-04-09 09:56:16 +0200401 *
402 * Warning: this is work in progress, some tests may not be reliable enough
403 * yet! False positives may happen.
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100404 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405int mbedtls_timing_self_test( int verbose )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100406{
Gilles Peskine105e6bc2017-10-10 20:09:26 +0200407 unsigned long cycles = 0, ratio = 0;
408 unsigned long millisecs = 0, secs = 0;
409 int hardfail = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 struct mbedtls_timing_hr_time hires;
Gilles Peskine105e6bc2017-10-10 20:09:26 +0200411 uint32_t a = 0, b = 0;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200412 mbedtls_timing_delay_context ctx;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100413
Paul Bakker66d5d072014-06-17 16:39:18 +0200414 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_printf( " TIMING tests note: will take some time!\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100416
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100417 if( verbose != 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200418 mbedtls_printf( " TIMING test #1 (set_alarm / get_timer): " );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100419
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100420 {
Gilles Peskine36816922017-12-20 22:32:47 +0100421 secs = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_set_alarm( (int) secs );
425 while( !mbedtls_timing_alarmed )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100426 ;
427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 millisecs = mbedtls_timing_get_timer( &hires, 0 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100429
Manuel Pégourié-Gonnarde578b1c2015-08-18 20:11:48 +0200430 /* For some reason on Windows it looks like alarm has an extra delay
431 * (maybe related to creating a new thread). Allow some room here. */
432 if( millisecs < 800 * secs || millisecs > 1200 * secs + 300 )
Gilles Peskine105e6bc2017-10-10 20:09:26 +0200433 FAIL;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100434 }
435
436 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100438
439 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200440 mbedtls_printf( " TIMING test #2 (set/get_delay ): " );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200441
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200442 {
Gilles Peskined3949622017-10-27 18:42:32 +0200443 a = 800;
444 b = 400;
445 mbedtls_timing_set_delay( &ctx, a, a + b ); /* T = 0 */
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200446
Gilles Peskined3949622017-10-27 18:42:32 +0200447 busy_msleep( a - a / 4 ); /* T = a - a/4 */
448 if( mbedtls_timing_get_delay( &ctx ) != 0 )
449 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200450
Gilles Peskined3949622017-10-27 18:42:32 +0200451 busy_msleep( a / 4 + b / 4 ); /* T = a + b/4 */
452 if( mbedtls_timing_get_delay( &ctx ) != 1 )
453 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200454
Gilles Peskined3949622017-10-27 18:42:32 +0200455 busy_msleep( b ); /* T = a + b + b/4 */
456 if( mbedtls_timing_get_delay( &ctx ) != 2 )
457 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200458 }
459
460 mbedtls_timing_set_delay( &ctx, 0, 0 );
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200461 busy_msleep( 200 );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200462 if( mbedtls_timing_get_delay( &ctx ) != -1 )
463 FAIL;
464
465 if( verbose != 0 )
466 mbedtls_printf( "passed\n" );
467
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200468 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200469 mbedtls_printf( " TIMING test #3 (hardclock / get_timer): " );
470
471 /*
472 * Allow one failure for possible counter wrapping.
473 * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
474 * since the whole test is about 10ms, it shouldn't happen twice in a row.
475 */
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200476
477hard_test:
478 if( hardfail > 1 )
479 {
480 if( verbose != 0 )
481 mbedtls_printf( "failed (ignored)\n" );
482
483 goto hard_test_done;
484 }
485
486 /* Get a reference ratio cycles/ms */
487 millisecs = 1;
488 cycles = mbedtls_timing_hardclock();
489 busy_msleep( millisecs );
490 cycles = mbedtls_timing_hardclock() - cycles;
491 ratio = cycles / millisecs;
492
493 /* Check that the ratio is mostly constant */
494 for( millisecs = 2; millisecs <= 4; millisecs++ )
495 {
496 cycles = mbedtls_timing_hardclock();
497 busy_msleep( millisecs );
498 cycles = mbedtls_timing_hardclock() - cycles;
499
500 /* Allow variation up to 20% */
501 if( cycles / millisecs < ratio - ratio / 5 ||
502 cycles / millisecs > ratio + ratio / 5 )
503 {
504 hardfail++;
505 goto hard_test;
506 }
507 }
508
509 if( verbose != 0 )
510 mbedtls_printf( "passed\n" );
511
512hard_test_done:
513
514 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200516
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100517 return( 0 );
518}
519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100521
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200522#endif /* MBEDTLS_TIMING_C */