blob: 28bef5579857a144085a7b62229488066ca44d0b [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>
irwir7f244a52018-06-23 18:55:14 +030049#include <process.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000050
51struct _hr_time
52{
53 LARGE_INTEGER start;
54};
55
56#else
57
58#include <unistd.h>
59#include <sys/types.h>
60#include <sys/time.h>
61#include <signal.h>
62#include <time.h>
63
64struct _hr_time
65{
66 struct timeval start;
67};
68
Paul Bakker9af723c2014-05-01 13:03:14 +020069#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020071#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +020072 ( defined(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__)
Paul Bakker5121ce52009-01-03 21:22:43 +000073
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020074#define HAVE_HARDCLOCK
Paul Bakkerbb0139c2012-10-31 09:53:08 +000075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +000077{
78 unsigned long tsc;
79 __asm rdtsc
80 __asm mov [tsc], eax
81 return( tsc );
82}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020083#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +020084 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
Paul Bakker5121ce52009-01-03 21:22:43 +000085
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +000086/* some versions of mingw-64 have 32-bit longs even on x84_64 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020087#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +000088 defined(__GNUC__) && ( defined(__i386__) || ( \
89 ( defined(__amd64__) || defined( __x86_64__) ) && __SIZEOF_LONG__ == 4 ) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +000090
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020091#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +000092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +000094{
Paul Bakkerca410102011-10-19 14:27:36 +000095 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +010096 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakkerca410102011-10-19 14:27:36 +000097 return( lo );
Paul Bakker5121ce52009-01-03 21:22:43 +000098}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +020099#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200100 __GNUC__ && __i386__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200102#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +0200103 defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000104
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200105#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000108{
109 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100110 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200111 return( lo | ( hi << 32 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000112}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200113#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200114 __GNUC__ && ( __amd64__ || __x86_64__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200116#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +0200117 defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000118
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200119#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000122{
123 unsigned long tbl, tbu0, tbu1;
124
125 do
126 {
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100127 asm volatile( "mftbu %0" : "=r" (tbu0) );
128 asm volatile( "mftb %0" : "=r" (tbl ) );
129 asm volatile( "mftbu %0" : "=r" (tbu1) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 }
131 while( tbu0 != tbu1 );
132
133 return( tbl );
134}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200135#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200136 __GNUC__ && ( __powerpc__ || __ppc__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200138#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000139 defined(__GNUC__) && defined(__sparc64__)
140
141#if defined(__OpenBSD__)
142#warning OpenBSD does not allow access to tick register using software version instead
Paul Bakker5121ce52009-01-03 21:22:43 +0000143#else
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200144#define HAVE_HARDCLOCK
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146unsigned long mbedtls_timing_hardclock( void )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000147{
148 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100149 asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) );
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000150 return( tick );
151}
Paul Bakker9af723c2014-05-01 13:03:14 +0200152#endif /* __OpenBSD__ */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200153#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200154 __GNUC__ && __sparc64__ */
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000155
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200156#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000157 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
158
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200159#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000162{
163 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100164 asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" );
165 asm volatile( "mov %%g1, %0" : "=r" (tick) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 return( tick );
167}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200168#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200169 __GNUC__ && __sparc__ && !__sparc64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200171#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000172 defined(__GNUC__) && defined(__alpha__)
173
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200174#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000177{
178 unsigned long cc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100179 asm volatile( "rpcc %0" : "=r" (cc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 return( cc & 0xFFFFFFFF );
181}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200182#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200183 __GNUC__ && __alpha__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200185#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000186 defined(__GNUC__) && defined(__ia64__)
187
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200188#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000191{
192 unsigned long itc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100193 asm volatile( "mov %0 = ar.itc" : "=r" (itc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 return( itc );
195}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200196#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200197 __GNUC__ && __ia64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200199#if !defined(HAVE_HARDCLOCK) && defined(_MSC_VER) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100200 !defined(EFIX64) && !defined(EFI32)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000201
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200202#define HAVE_HARDCLOCK
Paul Bakker2eee9022011-04-24 15:28:55 +0000203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204unsigned long mbedtls_timing_hardclock( void )
Paul Bakker2eee9022011-04-24 15:28:55 +0000205{
206 LARGE_INTEGER offset;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100207
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100208 QueryPerformanceCounter( &offset );
Paul Bakker2eee9022011-04-24 15:28:55 +0000209
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200210 return( (unsigned long)( offset.QuadPart ) );
Paul Bakker2eee9022011-04-24 15:28:55 +0000211}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200212#endif /* !HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
Paul Bakker2eee9022011-04-24 15:28:55 +0000213
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200214#if !defined(HAVE_HARDCLOCK)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000215
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200216#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
218static int hardclock_init = 0;
219static struct timeval tv_init;
220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000222{
223 struct timeval tv_cur;
224
225 if( hardclock_init == 0 )
226 {
227 gettimeofday( &tv_init, NULL );
228 hardclock_init = 1;
229 }
230
231 gettimeofday( &tv_cur, NULL );
232 return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
233 + ( tv_cur.tv_usec - tv_init.tv_usec ) );
234}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200235#endif /* !HAVE_HARDCLOCK */
Paul Bakker5121ce52009-01-03 21:22:43 +0000236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237volatile int mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000238
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100239#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000242{
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 struct _hr_time *t = (struct _hr_time *) val;
244
Paul Bakker5121ce52009-01-03 21:22:43 +0000245 if( reset )
Gilles Peskineb29e70b2017-10-16 19:33:06 +0200246 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 QueryPerformanceCounter( &t->start );
Gilles Peskineb29e70b2017-10-16 19:33:06 +0200248 return( 0 );
249 }
250 else
251 {
252 unsigned long delta;
253 LARGE_INTEGER now, hfreq;
254 QueryPerformanceCounter( &now );
255 QueryPerformanceFrequency( &hfreq );
256 delta = (unsigned long)( ( now.QuadPart - t->start.QuadPart ) * 1000ul
257 / hfreq.QuadPart );
258 return( delta );
259 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000260}
261
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000262/* It's OK to use a global because alarm() is supposed to be global anyway */
263static DWORD alarmMs;
264
irwir7f244a52018-06-23 18:55:14 +0300265static void TimerProc( void *TimerContext )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100266{
irwir7f244a52018-06-23 18:55:14 +0300267 (void)TimerContext;
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000268 Sleep( alarmMs );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_timing_alarmed = 1;
irwir7f244a52018-06-23 18:55:14 +0300270 // Implicit call of _endthread() is better (see MS online docs)
Paul Bakker5121ce52009-01-03 21:22:43 +0000271}
272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273void mbedtls_set_alarm( int seconds )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100274{
Manuel Pégourié-Gonnard3e6222d2018-01-29 10:16:30 +0100275 if( seconds == 0 )
276 {
277 /* No need to create a thread for this simple case.
278 * Also, this shorcut is more reliable at least on MinGW32 */
279 mbedtls_timing_alarmed = 1;
280 return;
281 }
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_timing_alarmed = 0;
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000284 alarmMs = seconds * 1000;
irwir7f244a52018-06-23 18:55:14 +0300285 (void)_beginthread( TimerProc, 0, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000286}
287
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100288#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000291{
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 struct _hr_time *t = (struct _hr_time *) val;
293
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 if( reset )
295 {
Gilles Peskineb29e70b2017-10-16 19:33:06 +0200296 gettimeofday( &t->start, NULL );
Alfred Klompb308dd72014-07-14 22:32:21 +0200297 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 }
Gilles Peskineb29e70b2017-10-16 19:33:06 +0200299 else
300 {
301 unsigned long delta;
302 struct timeval now;
303 gettimeofday( &now, NULL );
304 delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul
305 + ( now.tv_usec - t->start.tv_usec ) / 1000;
306 return( delta );
307 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000308}
309
310static void sighandler( int signum )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100311{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 mbedtls_timing_alarmed = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 signal( signum, sighandler );
314}
315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316void mbedtls_set_alarm( int seconds )
Paul Bakker5121ce52009-01-03 21:22:43 +0000317{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 signal( SIGALRM, sighandler );
320 alarm( seconds );
Gilles Peskine3099b432017-10-10 20:10:46 +0200321 if( seconds == 0 )
322 {
323 /* alarm(0) cancelled any previous pending alarm, but the
324 handler won't fire, so raise the flag straight away. */
325 mbedtls_timing_alarmed = 1;
326 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000327}
328
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100329#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200331/*
332 * Set delays to watch
333 */
334void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms )
335{
336 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
337
338 ctx->int_ms = int_ms;
339 ctx->fin_ms = fin_ms;
340
341 if( fin_ms != 0 )
342 (void) mbedtls_timing_get_timer( &ctx->timer, 1 );
343}
344
345/*
346 * Get number of delays expired
347 */
348int mbedtls_timing_get_delay( void *data )
349{
350 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
351 unsigned long elapsed_ms;
352
353 if( ctx->fin_ms == 0 )
354 return( -1 );
355
356 elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 );
357
358 if( elapsed_ms >= ctx->fin_ms )
359 return( 2 );
360
361 if( elapsed_ms >= ctx->int_ms )
362 return( 1 );
363
364 return( 0 );
365}
366
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200367#endif /* !MBEDTLS_TIMING_ALT */
368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100370
371/*
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200372 * Busy-waits for the given number of milliseconds.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 * Used for testing mbedtls_timing_hardclock.
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200374 */
375static void busy_msleep( unsigned long msec )
376{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200378 unsigned long i = 0; /* for busy-waiting */
379 volatile unsigned long j; /* to prevent optimisation */
380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 while( mbedtls_timing_get_timer( &hires, 0 ) < msec )
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200384 i++;
385
386 j = i;
387 (void) j;
388}
389
Gilles Peskine105e6bc2017-10-10 20:09:26 +0200390#define FAIL do \
391 { \
392 if( verbose != 0 ) \
393 { \
394 mbedtls_printf( "failed at line %d\n", __LINE__ ); \
395 mbedtls_printf( " cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d a=%lu b=%lu\n", \
396 cycles, ratio, millisecs, secs, hardfail, \
397 (unsigned long) a, (unsigned long) b ); \
398 mbedtls_printf( " elapsed(hires)=%lu elapsed(ctx)=%lu status(ctx)=%d\n", \
399 mbedtls_timing_get_timer( &hires, 0 ), \
400 mbedtls_timing_get_timer( &ctx.timer, 0 ), \
401 mbedtls_timing_get_delay( &ctx ) ); \
402 } \
403 return( 1 ); \
404 } while( 0 )
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200405
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200406/*
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100407 * Checkup routine
Manuel Pégourié-Gonnard0f79bab2014-04-09 09:56:16 +0200408 *
409 * Warning: this is work in progress, some tests may not be reliable enough
410 * yet! False positives may happen.
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412int mbedtls_timing_self_test( int verbose )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100413{
Gilles Peskine105e6bc2017-10-10 20:09:26 +0200414 unsigned long cycles = 0, ratio = 0;
415 unsigned long millisecs = 0, secs = 0;
416 int hardfail = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 struct mbedtls_timing_hr_time hires;
Gilles Peskine105e6bc2017-10-10 20:09:26 +0200418 uint32_t a = 0, b = 0;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200419 mbedtls_timing_delay_context ctx;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100420
Paul Bakker66d5d072014-06-17 16:39:18 +0200421 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_printf( " TIMING tests note: will take some time!\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100423
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100424 if( verbose != 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200425 mbedtls_printf( " TIMING test #1 (set_alarm / get_timer): " );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100426
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100427 {
Gilles Peskine36816922017-12-20 22:32:47 +0100428 secs = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 mbedtls_set_alarm( (int) secs );
432 while( !mbedtls_timing_alarmed )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100433 ;
434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 millisecs = mbedtls_timing_get_timer( &hires, 0 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100436
Manuel Pégourié-Gonnarde578b1c2015-08-18 20:11:48 +0200437 /* For some reason on Windows it looks like alarm has an extra delay
438 * (maybe related to creating a new thread). Allow some room here. */
439 if( millisecs < 800 * secs || millisecs > 1200 * secs + 300 )
Gilles Peskine105e6bc2017-10-10 20:09:26 +0200440 FAIL;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100441 }
442
443 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100445
446 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200447 mbedtls_printf( " TIMING test #2 (set/get_delay ): " );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200448
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200449 {
Gilles Peskined3949622017-10-27 18:42:32 +0200450 a = 800;
451 b = 400;
452 mbedtls_timing_set_delay( &ctx, a, a + b ); /* T = 0 */
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200453
Gilles Peskined3949622017-10-27 18:42:32 +0200454 busy_msleep( a - a / 4 ); /* T = a - a/4 */
455 if( mbedtls_timing_get_delay( &ctx ) != 0 )
456 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200457
Gilles Peskined3949622017-10-27 18:42:32 +0200458 busy_msleep( a / 4 + b / 4 ); /* T = a + b/4 */
459 if( mbedtls_timing_get_delay( &ctx ) != 1 )
460 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200461
Gilles Peskined3949622017-10-27 18:42:32 +0200462 busy_msleep( b ); /* T = a + b + b/4 */
463 if( mbedtls_timing_get_delay( &ctx ) != 2 )
464 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200465 }
466
467 mbedtls_timing_set_delay( &ctx, 0, 0 );
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200468 busy_msleep( 200 );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200469 if( mbedtls_timing_get_delay( &ctx ) != -1 )
470 FAIL;
471
472 if( verbose != 0 )
473 mbedtls_printf( "passed\n" );
474
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200475 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200476 mbedtls_printf( " TIMING test #3 (hardclock / get_timer): " );
477
478 /*
479 * Allow one failure for possible counter wrapping.
480 * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
481 * since the whole test is about 10ms, it shouldn't happen twice in a row.
482 */
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200483
484hard_test:
485 if( hardfail > 1 )
486 {
487 if( verbose != 0 )
488 mbedtls_printf( "failed (ignored)\n" );
489
490 goto hard_test_done;
491 }
492
493 /* Get a reference ratio cycles/ms */
494 millisecs = 1;
495 cycles = mbedtls_timing_hardclock();
496 busy_msleep( millisecs );
497 cycles = mbedtls_timing_hardclock() - cycles;
498 ratio = cycles / millisecs;
499
500 /* Check that the ratio is mostly constant */
501 for( millisecs = 2; millisecs <= 4; millisecs++ )
502 {
503 cycles = mbedtls_timing_hardclock();
504 busy_msleep( millisecs );
505 cycles = mbedtls_timing_hardclock() - cycles;
506
507 /* Allow variation up to 20% */
508 if( cycles / millisecs < ratio - ratio / 5 ||
509 cycles / millisecs > ratio + ratio / 5 )
510 {
511 hardfail++;
512 goto hard_test;
513 }
514 }
515
516 if( verbose != 0 )
517 mbedtls_printf( "passed\n" );
518
519hard_test_done:
520
521 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200523
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100524 return( 0 );
525}
526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100528
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200529#endif /* MBEDTLS_TIMING_C */