blob: f0d1a7840b246e89c80cc5f3ffb40cf617b18973 [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) && \
42 !defined(__APPLE__) && !defined(_WIN32)
43#error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in config.h"
44#endif
45
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020046#ifndef asm
47#define asm __asm
48#endif
49
Paul Bakkerfa6a6202013-10-28 18:48:30 +010050#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000051
52#include <windows.h>
53#include <winbase.h>
54
55struct _hr_time
56{
57 LARGE_INTEGER start;
58};
59
60#else
61
62#include <unistd.h>
63#include <sys/types.h>
64#include <sys/time.h>
65#include <signal.h>
66#include <time.h>
67
68struct _hr_time
69{
70 struct timeval start;
71};
72
Paul Bakker9af723c2014-05-01 13:03:14 +020073#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000074
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020075#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +020076 ( defined(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__)
Paul Bakker5121ce52009-01-03 21:22:43 +000077
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020078#define HAVE_HARDCLOCK
Paul Bakkerbb0139c2012-10-31 09:53:08 +000079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +000081{
82 unsigned long tsc;
83 __asm rdtsc
84 __asm mov [tsc], eax
85 return( tsc );
86}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020087#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +020088 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
Paul Bakker5121ce52009-01-03 21:22:43 +000089
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +000090/* some versions of mingw-64 have 32-bit longs even on x84_64 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020091#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +000092 defined(__GNUC__) && ( defined(__i386__) || ( \
93 ( defined(__amd64__) || defined( __x86_64__) ) && __SIZEOF_LONG__ == 4 ) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +000094
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020095#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +000098{
Paul Bakkerca410102011-10-19 14:27:36 +000099 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100100 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakkerca410102011-10-19 14:27:36 +0000101 return( lo );
Paul Bakker5121ce52009-01-03 21:22:43 +0000102}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200103#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200104 __GNUC__ && __i386__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200106#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +0200107 defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000108
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200109#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000112{
113 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100114 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200115 return( lo | ( hi << 32 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000116}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200117#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200118 __GNUC__ && ( __amd64__ || __x86_64__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200120#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +0200121 defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000122
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200123#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000126{
127 unsigned long tbl, tbu0, tbu1;
128
129 do
130 {
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100131 asm volatile( "mftbu %0" : "=r" (tbu0) );
132 asm volatile( "mftb %0" : "=r" (tbl ) );
133 asm volatile( "mftbu %0" : "=r" (tbu1) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 }
135 while( tbu0 != tbu1 );
136
137 return( tbl );
138}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200139#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200140 __GNUC__ && ( __powerpc__ || __ppc__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200142#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000143 defined(__GNUC__) && defined(__sparc64__)
144
145#if defined(__OpenBSD__)
146#warning OpenBSD does not allow access to tick register using software version instead
Paul Bakker5121ce52009-01-03 21:22:43 +0000147#else
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200148#define HAVE_HARDCLOCK
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150unsigned long mbedtls_timing_hardclock( void )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000151{
152 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100153 asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) );
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000154 return( tick );
155}
Paul Bakker9af723c2014-05-01 13:03:14 +0200156#endif /* __OpenBSD__ */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200157#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200158 __GNUC__ && __sparc64__ */
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000159
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200160#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000161 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
162
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200163#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000166{
167 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100168 asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" );
169 asm volatile( "mov %%g1, %0" : "=r" (tick) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 return( tick );
171}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200172#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200173 __GNUC__ && __sparc__ && !__sparc64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200175#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000176 defined(__GNUC__) && defined(__alpha__)
177
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200178#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000181{
182 unsigned long cc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100183 asm volatile( "rpcc %0" : "=r" (cc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 return( cc & 0xFFFFFFFF );
185}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200186#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200187 __GNUC__ && __alpha__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200189#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000190 defined(__GNUC__) && defined(__ia64__)
191
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200192#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000195{
196 unsigned long itc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100197 asm volatile( "mov %0 = ar.itc" : "=r" (itc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 return( itc );
199}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200200#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200201 __GNUC__ && __ia64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200203#if !defined(HAVE_HARDCLOCK) && defined(_MSC_VER) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100204 !defined(EFIX64) && !defined(EFI32)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000205
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200206#define HAVE_HARDCLOCK
Paul Bakker2eee9022011-04-24 15:28:55 +0000207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208unsigned long mbedtls_timing_hardclock( void )
Paul Bakker2eee9022011-04-24 15:28:55 +0000209{
210 LARGE_INTEGER offset;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100211
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100212 QueryPerformanceCounter( &offset );
Paul Bakker2eee9022011-04-24 15:28:55 +0000213
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200214 return( (unsigned long)( offset.QuadPart ) );
Paul Bakker2eee9022011-04-24 15:28:55 +0000215}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200216#endif /* !HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
Paul Bakker2eee9022011-04-24 15:28:55 +0000217
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200218#if !defined(HAVE_HARDCLOCK)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000219
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200220#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
222static int hardclock_init = 0;
223static struct timeval tv_init;
224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000226{
227 struct timeval tv_cur;
228
229 if( hardclock_init == 0 )
230 {
231 gettimeofday( &tv_init, NULL );
232 hardclock_init = 1;
233 }
234
235 gettimeofday( &tv_cur, NULL );
236 return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
237 + ( tv_cur.tv_usec - tv_init.tv_usec ) );
238}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200239#endif /* !HAVE_HARDCLOCK */
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241volatile int mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000242
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100243#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000246{
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 struct _hr_time *t = (struct _hr_time *) val;
248
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 if( reset )
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200250 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 QueryPerformanceCounter( &t->start );
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200252 return( 0 );
253 }
254 else
255 {
256 unsigned long delta;
257 LARGE_INTEGER now, hfreq;
258 QueryPerformanceCounter( &now );
259 QueryPerformanceFrequency( &hfreq );
260 delta = (unsigned long)( ( now.QuadPart - t->start.QuadPart ) * 1000ul
261 / hfreq.QuadPart );
262 return( delta );
263 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000264}
265
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000266/* It's OK to use a global because alarm() is supposed to be global anyway */
267static DWORD alarmMs;
268
Manuel Pégourié-Gonnard6d71e4e2015-02-11 12:54:35 +0000269static DWORD WINAPI TimerProc( LPVOID TimerContext )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100270{
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000271 ((void) TimerContext);
272 Sleep( alarmMs );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 mbedtls_timing_alarmed = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 return( TRUE );
275}
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277void mbedtls_set_alarm( int seconds )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100278{
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 DWORD ThreadId;
280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_timing_alarmed = 0;
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000282 alarmMs = seconds * 1000;
283 CloseHandle( CreateThread( NULL, 0, TimerProc, NULL, 0, &ThreadId ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000284}
285
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100286#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000289{
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 struct _hr_time *t = (struct _hr_time *) val;
291
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 if( reset )
293 {
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200294 gettimeofday( &t->start, NULL );
Alfred Klompb308dd72014-07-14 22:32:21 +0200295 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000296 }
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200297 else
298 {
299 unsigned long delta;
300 struct timeval now;
301 gettimeofday( &now, NULL );
302 delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul
303 + ( now.tv_usec - t->start.tv_usec ) / 1000;
304 return( delta );
305 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000306}
307
308static void sighandler( int signum )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100309{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 mbedtls_timing_alarmed = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000311 signal( signum, sighandler );
312}
313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314void mbedtls_set_alarm( int seconds )
Paul Bakker5121ce52009-01-03 21:22:43 +0000315{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 signal( SIGALRM, sighandler );
318 alarm( seconds );
Gilles Peskinea0af95f2017-10-10 20:10:46 +0200319 if( seconds == 0 )
320 {
321 /* alarm(0) cancelled any previous pending alarm, but the
322 handler won't fire, so raise the flag straight away. */
323 mbedtls_timing_alarmed = 1;
324 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000325}
326
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100327#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000328
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200329/*
330 * Set delays to watch
331 */
332void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms )
333{
334 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
335
336 ctx->int_ms = int_ms;
337 ctx->fin_ms = fin_ms;
338
339 if( fin_ms != 0 )
340 (void) mbedtls_timing_get_timer( &ctx->timer, 1 );
341}
342
343/*
344 * Get number of delays expired
345 */
346int mbedtls_timing_get_delay( void *data )
347{
348 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
349 unsigned long elapsed_ms;
350
351 if( ctx->fin_ms == 0 )
352 return( -1 );
353
354 elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 );
355
356 if( elapsed_ms >= ctx->fin_ms )
357 return( 2 );
358
359 if( elapsed_ms >= ctx->int_ms )
360 return( 1 );
361
362 return( 0 );
363}
364
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200365#endif /* !MBEDTLS_TIMING_ALT */
366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100368
369/*
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200370 * Busy-waits for the given number of milliseconds.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 * Used for testing mbedtls_timing_hardclock.
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200372 */
373static void busy_msleep( unsigned long msec )
374{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200376 unsigned long i = 0; /* for busy-waiting */
377 volatile unsigned long j; /* to prevent optimisation */
378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 while( mbedtls_timing_get_timer( &hires, 0 ) < msec )
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200382 i++;
383
384 j = i;
385 (void) j;
386}
387
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200388#define FAIL do \
389 { \
390 if( verbose != 0 ) \
391 { \
392 mbedtls_printf( "failed at line %d\n", __LINE__ ); \
393 mbedtls_printf( " cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d a=%lu b=%lu\n", \
394 cycles, ratio, millisecs, secs, hardfail, \
395 (unsigned long) a, (unsigned long) b ); \
396 mbedtls_printf( " elapsed(hires)=%lu elapsed(ctx)=%lu status(ctx)=%d\n", \
397 mbedtls_timing_get_timer( &hires, 0 ), \
398 mbedtls_timing_get_timer( &ctx.timer, 0 ), \
399 mbedtls_timing_get_delay( &ctx ) ); \
400 } \
401 return( 1 ); \
402 } while( 0 )
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200403
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200404/*
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100405 * Checkup routine
Manuel Pégourié-Gonnard0f79bab2014-04-09 09:56:16 +0200406 *
407 * Warning: this is work in progress, some tests may not be reliable enough
408 * yet! False positives may happen.
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410int mbedtls_timing_self_test( int verbose )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100411{
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200412 unsigned long cycles = 0, ratio = 0;
413 unsigned long millisecs = 0, secs = 0;
414 int hardfail = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 struct mbedtls_timing_hr_time hires;
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200416 uint32_t a = 0, b = 0;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200417 mbedtls_timing_delay_context ctx;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100418
Paul Bakker66d5d072014-06-17 16:39:18 +0200419 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_printf( " TIMING tests note: will take some time!\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100421
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100422 if( verbose != 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200423 mbedtls_printf( " TIMING test #1 (set_alarm / get_timer): " );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100424
425 for( secs = 1; secs <= 3; secs++ )
426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_set_alarm( (int) secs );
430 while( !mbedtls_timing_alarmed )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100431 ;
432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 millisecs = mbedtls_timing_get_timer( &hires, 0 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100434
Manuel Pégourié-Gonnarde578b1c2015-08-18 20:11:48 +0200435 /* For some reason on Windows it looks like alarm has an extra delay
436 * (maybe related to creating a new thread). Allow some room here. */
437 if( millisecs < 800 * secs || millisecs > 1200 * secs + 300 )
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200438 FAIL;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100439 }
440
441 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100443
444 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200445 mbedtls_printf( " TIMING test #2 (set/get_delay ): " );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200446
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200447 {
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200448 a = 800;
449 b = 400;
450 mbedtls_timing_set_delay( &ctx, a, a + b ); /* T = 0 */
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200451
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200452 busy_msleep( a - a / 4 ); /* T = a - a/4 */
453 if( mbedtls_timing_get_delay( &ctx ) != 0 )
454 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200455
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200456 busy_msleep( a / 4 + b / 4 ); /* T = a + b/4 */
457 if( mbedtls_timing_get_delay( &ctx ) != 1 )
458 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200459
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200460 busy_msleep( b ); /* T = a + b + b/4 */
461 if( mbedtls_timing_get_delay( &ctx ) != 2 )
462 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200463 }
464
465 mbedtls_timing_set_delay( &ctx, 0, 0 );
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200466 busy_msleep( 200 );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200467 if( mbedtls_timing_get_delay( &ctx ) != -1 )
468 FAIL;
469
470 if( verbose != 0 )
471 mbedtls_printf( "passed\n" );
472
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200473 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200474 mbedtls_printf( " TIMING test #3 (hardclock / get_timer): " );
475
476 /*
477 * Allow one failure for possible counter wrapping.
478 * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
479 * since the whole test is about 10ms, it shouldn't happen twice in a row.
480 */
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200481
482hard_test:
483 if( hardfail > 1 )
484 {
485 if( verbose != 0 )
486 mbedtls_printf( "failed (ignored)\n" );
487
488 goto hard_test_done;
489 }
490
491 /* Get a reference ratio cycles/ms */
492 millisecs = 1;
493 cycles = mbedtls_timing_hardclock();
494 busy_msleep( millisecs );
495 cycles = mbedtls_timing_hardclock() - cycles;
496 ratio = cycles / millisecs;
497
498 /* Check that the ratio is mostly constant */
499 for( millisecs = 2; millisecs <= 4; millisecs++ )
500 {
501 cycles = mbedtls_timing_hardclock();
502 busy_msleep( millisecs );
503 cycles = mbedtls_timing_hardclock() - cycles;
504
505 /* Allow variation up to 20% */
506 if( cycles / millisecs < ratio - ratio / 5 ||
507 cycles / millisecs > ratio + ratio / 5 )
508 {
509 hardfail++;
510 goto hard_test;
511 }
512 }
513
514 if( verbose != 0 )
515 mbedtls_printf( "passed\n" );
516
517hard_test_done:
518
519 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200521
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100522 return( 0 );
523}
524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100526
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200527#endif /* MBEDTLS_TIMING_C */