blob: 90cfe88ed8a24022a12edff621b0f20b91b7bd0d [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
Gilles Peskinedb09ef62020-06-03 01:43:33 +020022#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000025#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010026#else
27#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#define mbedtls_printf printf
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010029#endif
30
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +020031#if defined(MBEDTLS_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +020035#if !defined(MBEDTLS_TIMING_ALT)
36
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010037#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040038 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
Ørjan Malde479d8de2020-05-20 09:32:39 +000039 !defined(__HAIKU__) && !defined(__midipix__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010040#error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in config.h"
41#endif
42
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020043#ifndef asm
44#define asm __asm
45#endif
46
Paul Bakkerfa6a6202013-10-28 18:48:30 +010047#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000048
49#include <windows.h>
irwire931d0e2018-06-23 18:55:14 +030050#include <process.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000051
52struct _hr_time
53{
54 LARGE_INTEGER start;
55};
56
57#else
58
59#include <unistd.h>
60#include <sys/types.h>
61#include <sys/time.h>
62#include <signal.h>
63#include <time.h>
64
65struct _hr_time
66{
67 struct timeval start;
68};
69
Paul Bakker9af723c2014-05-01 13:03:14 +020070#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000071
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020072#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +020073 ( defined(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__)
Paul Bakker5121ce52009-01-03 21:22:43 +000074
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020075#define HAVE_HARDCLOCK
Paul Bakkerbb0139c2012-10-31 09:53:08 +000076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +000078{
79 unsigned long tsc;
80 __asm rdtsc
81 __asm mov [tsc], eax
82 return( tsc );
83}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020084#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +020085 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
Paul Bakker5121ce52009-01-03 21:22:43 +000086
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +000087/* some versions of mingw-64 have 32-bit longs even on x84_64 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020088#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +000089 defined(__GNUC__) && ( defined(__i386__) || ( \
90 ( defined(__amd64__) || defined( __x86_64__) ) && __SIZEOF_LONG__ == 4 ) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +000091
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020092#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +000093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +000095{
Paul Bakkerca410102011-10-19 14:27:36 +000096 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +010097 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakkerca410102011-10-19 14:27:36 +000098 return( lo );
Paul Bakker5121ce52009-01-03 21:22:43 +000099}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200100#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200101 __GNUC__ && __i386__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200103#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +0200104 defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000105
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200106#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000109{
110 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100111 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200112 return( lo | ( hi << 32 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000113}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200114#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200115 __GNUC__ && ( __amd64__ || __x86_64__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200117#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +0200118 defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000119
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200120#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000123{
124 unsigned long tbl, tbu0, tbu1;
125
126 do
127 {
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100128 asm volatile( "mftbu %0" : "=r" (tbu0) );
129 asm volatile( "mftb %0" : "=r" (tbl ) );
130 asm volatile( "mftbu %0" : "=r" (tbu1) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 }
132 while( tbu0 != tbu1 );
133
134 return( tbl );
135}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200136#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200137 __GNUC__ && ( __powerpc__ || __ppc__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200139#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000140 defined(__GNUC__) && defined(__sparc64__)
141
142#if defined(__OpenBSD__)
143#warning OpenBSD does not allow access to tick register using software version instead
Paul Bakker5121ce52009-01-03 21:22:43 +0000144#else
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200145#define HAVE_HARDCLOCK
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147unsigned long mbedtls_timing_hardclock( void )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000148{
149 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100150 asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) );
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000151 return( tick );
152}
Paul Bakker9af723c2014-05-01 13:03:14 +0200153#endif /* __OpenBSD__ */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200154#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200155 __GNUC__ && __sparc64__ */
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000156
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200157#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000158 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
159
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200160#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000163{
164 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100165 asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" );
166 asm volatile( "mov %%g1, %0" : "=r" (tick) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 return( tick );
168}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200169#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200170 __GNUC__ && __sparc__ && !__sparc64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000171
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200172#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000173 defined(__GNUC__) && defined(__alpha__)
174
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200175#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000178{
179 unsigned long cc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100180 asm volatile( "rpcc %0" : "=r" (cc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 return( cc & 0xFFFFFFFF );
182}
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__ && __alpha__ */
Paul Bakker5121ce52009-01-03 21:22:43 +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(__ia64__)
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 itc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100194 asm volatile( "mov %0 = ar.itc" : "=r" (itc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000195 return( itc );
196}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200197#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200198 __GNUC__ && __ia64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000199
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200200#if !defined(HAVE_HARDCLOCK) && defined(_MSC_VER) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100201 !defined(EFIX64) && !defined(EFI32)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000202
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200203#define HAVE_HARDCLOCK
Paul Bakker2eee9022011-04-24 15:28:55 +0000204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205unsigned long mbedtls_timing_hardclock( void )
Paul Bakker2eee9022011-04-24 15:28:55 +0000206{
207 LARGE_INTEGER offset;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100208
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100209 QueryPerformanceCounter( &offset );
Paul Bakker2eee9022011-04-24 15:28:55 +0000210
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200211 return( (unsigned long)( offset.QuadPart ) );
Paul Bakker2eee9022011-04-24 15:28:55 +0000212}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200213#endif /* !HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
Paul Bakker2eee9022011-04-24 15:28:55 +0000214
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200215#if !defined(HAVE_HARDCLOCK)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000216
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200217#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000218
219static int hardclock_init = 0;
220static struct timeval tv_init;
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000223{
224 struct timeval tv_cur;
225
226 if( hardclock_init == 0 )
227 {
228 gettimeofday( &tv_init, NULL );
229 hardclock_init = 1;
230 }
231
232 gettimeofday( &tv_cur, NULL );
233 return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
234 + ( tv_cur.tv_usec - tv_init.tv_usec ) );
235}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200236#endif /* !HAVE_HARDCLOCK */
Paul Bakker5121ce52009-01-03 21:22:43 +0000237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238volatile int mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100240#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000243{
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 struct _hr_time *t = (struct _hr_time *) val;
245
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 if( reset )
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200247 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 QueryPerformanceCounter( &t->start );
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200249 return( 0 );
250 }
251 else
252 {
253 unsigned long delta;
254 LARGE_INTEGER now, hfreq;
255 QueryPerformanceCounter( &now );
256 QueryPerformanceFrequency( &hfreq );
257 delta = (unsigned long)( ( now.QuadPart - t->start.QuadPart ) * 1000ul
258 / hfreq.QuadPart );
259 return( delta );
260 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000261}
262
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000263/* It's OK to use a global because alarm() is supposed to be global anyway */
264static DWORD alarmMs;
265
irwire931d0e2018-06-23 18:55:14 +0300266static void TimerProc( void *TimerContext )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100267{
irwire1b82ad2018-08-30 11:57:09 +0300268 (void) TimerContext;
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000269 Sleep( alarmMs );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_timing_alarmed = 1;
irwirda642d92018-08-31 15:14:54 +0300271 /* _endthread will be called implicitly on return
272 * That ensures execution of thread funcition's epilogue */
Paul Bakker5121ce52009-01-03 21:22:43 +0000273}
274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275void mbedtls_set_alarm( int seconds )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100276{
Manuel Pégourié-Gonnard54059622018-01-29 10:16:30 +0100277 if( seconds == 0 )
278 {
279 /* No need to create a thread for this simple case.
280 * Also, this shorcut is more reliable at least on MinGW32 */
281 mbedtls_timing_alarmed = 1;
282 return;
283 }
284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_timing_alarmed = 0;
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000286 alarmMs = seconds * 1000;
irwire1b82ad2018-08-30 11:57:09 +0300287 (void) _beginthread( TimerProc, 0, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000288}
289
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100290#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000293{
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 struct _hr_time *t = (struct _hr_time *) val;
295
Paul Bakker5121ce52009-01-03 21:22:43 +0000296 if( reset )
297 {
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200298 gettimeofday( &t->start, NULL );
Alfred Klompb308dd72014-07-14 22:32:21 +0200299 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000300 }
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200301 else
302 {
303 unsigned long delta;
304 struct timeval now;
305 gettimeofday( &now, NULL );
306 delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul
307 + ( now.tv_usec - t->start.tv_usec ) / 1000;
308 return( delta );
309 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000310}
311
312static void sighandler( int signum )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100313{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 mbedtls_timing_alarmed = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000315 signal( signum, sighandler );
316}
317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318void mbedtls_set_alarm( int seconds )
Paul Bakker5121ce52009-01-03 21:22:43 +0000319{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000321 signal( SIGALRM, sighandler );
322 alarm( seconds );
Gilles Peskinea0af95f2017-10-10 20:10:46 +0200323 if( seconds == 0 )
324 {
325 /* alarm(0) cancelled any previous pending alarm, but the
326 handler won't fire, so raise the flag straight away. */
327 mbedtls_timing_alarmed = 1;
328 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000329}
330
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100331#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000332
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200333/*
334 * Set delays to watch
335 */
336void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms )
337{
338 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
339
340 ctx->int_ms = int_ms;
341 ctx->fin_ms = fin_ms;
342
343 if( fin_ms != 0 )
344 (void) mbedtls_timing_get_timer( &ctx->timer, 1 );
345}
346
347/*
348 * Get number of delays expired
349 */
350int mbedtls_timing_get_delay( void *data )
351{
352 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
353 unsigned long elapsed_ms;
354
355 if( ctx->fin_ms == 0 )
356 return( -1 );
357
358 elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 );
359
360 if( elapsed_ms >= ctx->fin_ms )
361 return( 2 );
362
363 if( elapsed_ms >= ctx->int_ms )
364 return( 1 );
365
366 return( 0 );
367}
368
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200369#endif /* !MBEDTLS_TIMING_ALT */
370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100372
373/*
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200374 * Busy-waits for the given number of milliseconds.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 * Used for testing mbedtls_timing_hardclock.
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200376 */
377static void busy_msleep( unsigned long msec )
378{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200380 unsigned long i = 0; /* for busy-waiting */
381 volatile unsigned long j; /* to prevent optimisation */
382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 while( mbedtls_timing_get_timer( &hires, 0 ) < msec )
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200386 i++;
387
388 j = i;
389 (void) j;
390}
391
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200392#define FAIL do \
393 { \
394 if( verbose != 0 ) \
395 { \
396 mbedtls_printf( "failed at line %d\n", __LINE__ ); \
397 mbedtls_printf( " cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d a=%lu b=%lu\n", \
398 cycles, ratio, millisecs, secs, hardfail, \
399 (unsigned long) a, (unsigned long) b ); \
400 mbedtls_printf( " elapsed(hires)=%lu elapsed(ctx)=%lu status(ctx)=%d\n", \
401 mbedtls_timing_get_timer( &hires, 0 ), \
402 mbedtls_timing_get_timer( &ctx.timer, 0 ), \
403 mbedtls_timing_get_delay( &ctx ) ); \
404 } \
405 return( 1 ); \
406 } while( 0 )
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200407
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200408/*
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100409 * Checkup routine
Manuel Pégourié-Gonnard0f79bab2014-04-09 09:56:16 +0200410 *
411 * Warning: this is work in progress, some tests may not be reliable enough
412 * yet! False positives may happen.
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100413 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414int mbedtls_timing_self_test( int verbose )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100415{
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200416 unsigned long cycles = 0, ratio = 0;
417 unsigned long millisecs = 0, secs = 0;
418 int hardfail = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 struct mbedtls_timing_hr_time hires;
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200420 uint32_t a = 0, b = 0;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200421 mbedtls_timing_delay_context ctx;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100422
Paul Bakker66d5d072014-06-17 16:39:18 +0200423 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_printf( " TIMING tests note: will take some time!\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100425
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100426 if( verbose != 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200427 mbedtls_printf( " TIMING test #1 (set_alarm / get_timer): " );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100428
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100429 {
Gilles Peskineada3ee82017-12-20 22:31:17 +0100430 secs = 1;
431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_set_alarm( (int) secs );
435 while( !mbedtls_timing_alarmed )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100436 ;
437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 millisecs = mbedtls_timing_get_timer( &hires, 0 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100439
Manuel Pégourié-Gonnarde578b1c2015-08-18 20:11:48 +0200440 /* For some reason on Windows it looks like alarm has an extra delay
441 * (maybe related to creating a new thread). Allow some room here. */
442 if( millisecs < 800 * secs || millisecs > 1200 * secs + 300 )
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200443 FAIL;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100444 }
445
446 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100448
449 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200450 mbedtls_printf( " TIMING test #2 (set/get_delay ): " );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200451
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200452 {
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200453 a = 800;
454 b = 400;
455 mbedtls_timing_set_delay( &ctx, a, a + b ); /* T = 0 */
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200456
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200457 busy_msleep( a - a / 4 ); /* T = a - a/4 */
458 if( mbedtls_timing_get_delay( &ctx ) != 0 )
459 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200460
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200461 busy_msleep( a / 4 + b / 4 ); /* T = a + b/4 */
462 if( mbedtls_timing_get_delay( &ctx ) != 1 )
463 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200464
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200465 busy_msleep( b ); /* T = a + b + b/4 */
466 if( mbedtls_timing_get_delay( &ctx ) != 2 )
467 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200468 }
469
470 mbedtls_timing_set_delay( &ctx, 0, 0 );
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200471 busy_msleep( 200 );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200472 if( mbedtls_timing_get_delay( &ctx ) != -1 )
473 FAIL;
474
475 if( verbose != 0 )
476 mbedtls_printf( "passed\n" );
477
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200478 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200479 mbedtls_printf( " TIMING test #3 (hardclock / get_timer): " );
480
481 /*
482 * Allow one failure for possible counter wrapping.
483 * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
484 * since the whole test is about 10ms, it shouldn't happen twice in a row.
485 */
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200486
487hard_test:
488 if( hardfail > 1 )
489 {
490 if( verbose != 0 )
491 mbedtls_printf( "failed (ignored)\n" );
492
493 goto hard_test_done;
494 }
495
496 /* Get a reference ratio cycles/ms */
497 millisecs = 1;
498 cycles = mbedtls_timing_hardclock();
499 busy_msleep( millisecs );
500 cycles = mbedtls_timing_hardclock() - cycles;
501 ratio = cycles / millisecs;
502
503 /* Check that the ratio is mostly constant */
504 for( millisecs = 2; millisecs <= 4; millisecs++ )
505 {
506 cycles = mbedtls_timing_hardclock();
507 busy_msleep( millisecs );
508 cycles = mbedtls_timing_hardclock() - cycles;
509
510 /* Allow variation up to 20% */
511 if( cycles / millisecs < ratio - ratio / 5 ||
512 cycles / millisecs > ratio + ratio / 5 )
513 {
514 hardfail++;
515 goto hard_test;
516 }
517 }
518
519 if( verbose != 0 )
520 mbedtls_printf( "passed\n" );
521
522hard_test_done:
523
524 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200526
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100527 return( 0 );
528}
529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100531
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200532#endif /* MBEDTLS_TIMING_C */