blob: d9db5d2df3751b378057f249ec4c25ce18ac07b5 [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é-Gonnard3e6222d2018-01-29 10:16:30 +0100276 if( seconds == 0 )
277 {
278 /* No need to create a thread for this simple case.
279 * Also, this shorcut is more reliable at least on MinGW32 */
280 mbedtls_timing_alarmed = 1;
281 return;
282 }
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_timing_alarmed = 0;
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000285 alarmMs = seconds * 1000;
286 CloseHandle( CreateThread( NULL, 0, TimerProc, NULL, 0, &ThreadId ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000287}
288
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100289#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000292{
Paul Bakker5121ce52009-01-03 21:22:43 +0000293 struct _hr_time *t = (struct _hr_time *) val;
294
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 if( reset )
296 {
Gilles Peskineb29e70b2017-10-16 19:33:06 +0200297 gettimeofday( &t->start, NULL );
Alfred Klompb308dd72014-07-14 22:32:21 +0200298 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 }
Gilles Peskineb29e70b2017-10-16 19:33:06 +0200300 else
301 {
302 unsigned long delta;
303 struct timeval now;
304 gettimeofday( &now, NULL );
305 delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul
306 + ( now.tv_usec - t->start.tv_usec ) / 1000;
307 return( delta );
308 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000309}
310
311static void sighandler( int signum )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100312{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_timing_alarmed = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 signal( signum, sighandler );
315}
316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317void mbedtls_set_alarm( int seconds )
Paul Bakker5121ce52009-01-03 21:22:43 +0000318{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000320 signal( SIGALRM, sighandler );
321 alarm( seconds );
Gilles Peskine3099b432017-10-10 20:10:46 +0200322 if( seconds == 0 )
323 {
324 /* alarm(0) cancelled any previous pending alarm, but the
325 handler won't fire, so raise the flag straight away. */
326 mbedtls_timing_alarmed = 1;
327 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000328}
329
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100330#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000331
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200332/*
333 * Set delays to watch
334 */
335void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms )
336{
337 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
338
339 ctx->int_ms = int_ms;
340 ctx->fin_ms = fin_ms;
341
342 if( fin_ms != 0 )
343 (void) mbedtls_timing_get_timer( &ctx->timer, 1 );
344}
345
346/*
347 * Get number of delays expired
348 */
349int mbedtls_timing_get_delay( void *data )
350{
351 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
352 unsigned long elapsed_ms;
353
354 if( ctx->fin_ms == 0 )
355 return( -1 );
356
357 elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 );
358
359 if( elapsed_ms >= ctx->fin_ms )
360 return( 2 );
361
362 if( elapsed_ms >= ctx->int_ms )
363 return( 1 );
364
365 return( 0 );
366}
367
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200368#endif /* !MBEDTLS_TIMING_ALT */
369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100371
372/*
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200373 * Busy-waits for the given number of milliseconds.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 * Used for testing mbedtls_timing_hardclock.
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200375 */
376static void busy_msleep( unsigned long msec )
377{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200379 unsigned long i = 0; /* for busy-waiting */
380 volatile unsigned long j; /* to prevent optimisation */
381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 while( mbedtls_timing_get_timer( &hires, 0 ) < msec )
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200385 i++;
386
387 j = i;
388 (void) j;
389}
390
Gilles Peskine105e6bc2017-10-10 20:09:26 +0200391#define FAIL do \
392 { \
393 if( verbose != 0 ) \
394 { \
395 mbedtls_printf( "failed at line %d\n", __LINE__ ); \
396 mbedtls_printf( " cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d a=%lu b=%lu\n", \
397 cycles, ratio, millisecs, secs, hardfail, \
398 (unsigned long) a, (unsigned long) b ); \
399 mbedtls_printf( " elapsed(hires)=%lu elapsed(ctx)=%lu status(ctx)=%d\n", \
400 mbedtls_timing_get_timer( &hires, 0 ), \
401 mbedtls_timing_get_timer( &ctx.timer, 0 ), \
402 mbedtls_timing_get_delay( &ctx ) ); \
403 } \
404 return( 1 ); \
405 } while( 0 )
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200406
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200407/*
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100408 * Checkup routine
Manuel Pégourié-Gonnard0f79bab2014-04-09 09:56:16 +0200409 *
410 * Warning: this is work in progress, some tests may not be reliable enough
411 * yet! False positives may happen.
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100412 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413int mbedtls_timing_self_test( int verbose )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100414{
Gilles Peskine105e6bc2017-10-10 20:09:26 +0200415 unsigned long cycles = 0, ratio = 0;
416 unsigned long millisecs = 0, secs = 0;
417 int hardfail = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 struct mbedtls_timing_hr_time hires;
Gilles Peskine105e6bc2017-10-10 20:09:26 +0200419 uint32_t a = 0, b = 0;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200420 mbedtls_timing_delay_context ctx;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100421
Paul Bakker66d5d072014-06-17 16:39:18 +0200422 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_printf( " TIMING tests note: will take some time!\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100424
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100425 if( verbose != 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200426 mbedtls_printf( " TIMING test #1 (set_alarm / get_timer): " );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100427
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100428 {
Gilles Peskine36816922017-12-20 22:32:47 +0100429 secs = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_set_alarm( (int) secs );
433 while( !mbedtls_timing_alarmed )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100434 ;
435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 millisecs = mbedtls_timing_get_timer( &hires, 0 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100437
Manuel Pégourié-Gonnarde578b1c2015-08-18 20:11:48 +0200438 /* For some reason on Windows it looks like alarm has an extra delay
439 * (maybe related to creating a new thread). Allow some room here. */
440 if( millisecs < 800 * secs || millisecs > 1200 * secs + 300 )
Gilles Peskine105e6bc2017-10-10 20:09:26 +0200441 FAIL;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100442 }
443
444 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100446
447 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200448 mbedtls_printf( " TIMING test #2 (set/get_delay ): " );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200449
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200450 {
Gilles Peskined3949622017-10-27 18:42:32 +0200451 a = 800;
452 b = 400;
453 mbedtls_timing_set_delay( &ctx, a, a + b ); /* T = 0 */
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200454
Gilles Peskined3949622017-10-27 18:42:32 +0200455 busy_msleep( a - a / 4 ); /* T = a - a/4 */
456 if( mbedtls_timing_get_delay( &ctx ) != 0 )
457 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200458
Gilles Peskined3949622017-10-27 18:42:32 +0200459 busy_msleep( a / 4 + b / 4 ); /* T = a + b/4 */
460 if( mbedtls_timing_get_delay( &ctx ) != 1 )
461 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200462
Gilles Peskined3949622017-10-27 18:42:32 +0200463 busy_msleep( b ); /* T = a + b + b/4 */
464 if( mbedtls_timing_get_delay( &ctx ) != 2 )
465 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200466 }
467
468 mbedtls_timing_set_delay( &ctx, 0, 0 );
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200469 busy_msleep( 200 );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200470 if( mbedtls_timing_get_delay( &ctx ) != -1 )
471 FAIL;
472
473 if( verbose != 0 )
474 mbedtls_printf( "passed\n" );
475
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200476 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200477 mbedtls_printf( " TIMING test #3 (hardclock / get_timer): " );
478
479 /*
480 * Allow one failure for possible counter wrapping.
481 * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
482 * since the whole test is about 10ms, it shouldn't happen twice in a row.
483 */
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200484
485hard_test:
486 if( hardfail > 1 )
487 {
488 if( verbose != 0 )
489 mbedtls_printf( "failed (ignored)\n" );
490
491 goto hard_test_done;
492 }
493
494 /* Get a reference ratio cycles/ms */
495 millisecs = 1;
496 cycles = mbedtls_timing_hardclock();
497 busy_msleep( millisecs );
498 cycles = mbedtls_timing_hardclock() - cycles;
499 ratio = cycles / millisecs;
500
501 /* Check that the ratio is mostly constant */
502 for( millisecs = 2; millisecs <= 4; millisecs++ )
503 {
504 cycles = mbedtls_timing_hardclock();
505 busy_msleep( millisecs );
506 cycles = mbedtls_timing_hardclock() - cycles;
507
508 /* Allow variation up to 20% */
509 if( cycles / millisecs < ratio - ratio / 5 ||
510 cycles / millisecs > ratio + ratio / 5 )
511 {
512 hardfail++;
513 goto hard_test;
514 }
515 }
516
517 if( verbose != 0 )
518 mbedtls_printf( "passed\n" );
519
520hard_test_done:
521
522 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200524
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100525 return( 0 );
526}
527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100529
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200530#endif /* MBEDTLS_TIMING_C */