blob: a4beff35a9f7120b2ffb73643f6be9ffda863ac7 [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
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010057#else
58#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define mbedtls_printf printf
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010060#endif
61
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +020062#if defined(MBEDTLS_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000063
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +020066#if !defined(MBEDTLS_TIMING_ALT)
67
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010068#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040069 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
70 !defined(__HAIKU__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010071#error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in config.h"
72#endif
73
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020074#ifndef asm
75#define asm __asm
76#endif
77
Paul Bakkerfa6a6202013-10-28 18:48:30 +010078#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000079
80#include <windows.h>
irwire931d0e2018-06-23 18:55:14 +030081#include <process.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000082
83struct _hr_time
84{
85 LARGE_INTEGER start;
86};
87
88#else
89
90#include <unistd.h>
91#include <sys/types.h>
92#include <sys/time.h>
93#include <signal.h>
94#include <time.h>
95
96struct _hr_time
97{
98 struct timeval start;
99};
100
Paul Bakker9af723c2014-05-01 13:03:14 +0200101#endif /* _WIN32 && !EFIX64 && !EFI32 */
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(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__)
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200106#define HAVE_HARDCLOCK
Paul Bakkerbb0139c2012-10-31 09:53:08 +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 tsc;
111 __asm rdtsc
112 __asm mov [tsc], eax
113 return( tsc );
114}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200115#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200116 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +0000118/* some versions of mingw-64 have 32-bit longs even on x84_64 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200119#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +0000120 defined(__GNUC__) && ( defined(__i386__) || ( \
121 ( defined(__amd64__) || defined( __x86_64__) ) && __SIZEOF_LONG__ == 4 ) )
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{
Paul Bakkerca410102011-10-19 14:27:36 +0000127 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100128 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakkerca410102011-10-19 14:27:36 +0000129 return( lo );
Paul Bakker5121ce52009-01-03 21:22:43 +0000130}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200131#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200132 __GNUC__ && __i386__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000133
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200134#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +0200135 defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000136
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200137#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000140{
141 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100142 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200143 return( lo | ( hi << 32 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200145#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200146 __GNUC__ && ( __amd64__ || __x86_64__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000147
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200148#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +0200149 defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000150
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200151#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000154{
155 unsigned long tbl, tbu0, tbu1;
156
157 do
158 {
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100159 asm volatile( "mftbu %0" : "=r" (tbu0) );
160 asm volatile( "mftb %0" : "=r" (tbl ) );
161 asm volatile( "mftbu %0" : "=r" (tbu1) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 }
163 while( tbu0 != tbu1 );
164
165 return( tbl );
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__ && ( __powerpc__ || __ppc__ ) */
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(__sparc64__)
172
173#if defined(__OpenBSD__)
174#warning OpenBSD does not allow access to tick register using software version instead
Paul Bakker5121ce52009-01-03 21:22:43 +0000175#else
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200176#define HAVE_HARDCLOCK
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178unsigned long mbedtls_timing_hardclock( void )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000179{
180 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100181 asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) );
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000182 return( tick );
183}
Paul Bakker9af723c2014-05-01 13:03:14 +0200184#endif /* __OpenBSD__ */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200185#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200186 __GNUC__ && __sparc64__ */
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000187
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200188#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000189 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
190
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200191#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000194{
195 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100196 asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" );
197 asm volatile( "mov %%g1, %0" : "=r" (tick) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 return( tick );
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__ && __sparc__ && !__sparc64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200203#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000204 defined(__GNUC__) && defined(__alpha__)
205
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200206#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000209{
210 unsigned long cc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100211 asm volatile( "rpcc %0" : "=r" (cc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 return( cc & 0xFFFFFFFF );
213}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200214#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200215 __GNUC__ && __alpha__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200217#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000218 defined(__GNUC__) && defined(__ia64__)
219
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200220#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000223{
224 unsigned long itc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100225 asm volatile( "mov %0 = ar.itc" : "=r" (itc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 return( itc );
227}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200228#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200229 __GNUC__ && __ia64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200231#if !defined(HAVE_HARDCLOCK) && defined(_MSC_VER) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100232 !defined(EFIX64) && !defined(EFI32)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000233
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200234#define HAVE_HARDCLOCK
Paul Bakker2eee9022011-04-24 15:28:55 +0000235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236unsigned long mbedtls_timing_hardclock( void )
Paul Bakker2eee9022011-04-24 15:28:55 +0000237{
238 LARGE_INTEGER offset;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100239
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100240 QueryPerformanceCounter( &offset );
Paul Bakker2eee9022011-04-24 15:28:55 +0000241
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200242 return( (unsigned long)( offset.QuadPart ) );
Paul Bakker2eee9022011-04-24 15:28:55 +0000243}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200244#endif /* !HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
Paul Bakker2eee9022011-04-24 15:28:55 +0000245
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200246#if !defined(HAVE_HARDCLOCK)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000247
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200248#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000249
250static int hardclock_init = 0;
251static struct timeval tv_init;
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000254{
255 struct timeval tv_cur;
256
257 if( hardclock_init == 0 )
258 {
259 gettimeofday( &tv_init, NULL );
260 hardclock_init = 1;
261 }
262
263 gettimeofday( &tv_cur, NULL );
264 return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
265 + ( tv_cur.tv_usec - tv_init.tv_usec ) );
266}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200267#endif /* !HAVE_HARDCLOCK */
Paul Bakker5121ce52009-01-03 21:22:43 +0000268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269volatile int mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000270
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100271#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000274{
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 struct _hr_time *t = (struct _hr_time *) val;
276
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 if( reset )
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200278 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 QueryPerformanceCounter( &t->start );
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200280 return( 0 );
281 }
282 else
283 {
284 unsigned long delta;
285 LARGE_INTEGER now, hfreq;
286 QueryPerformanceCounter( &now );
287 QueryPerformanceFrequency( &hfreq );
288 delta = (unsigned long)( ( now.QuadPart - t->start.QuadPart ) * 1000ul
289 / hfreq.QuadPart );
290 return( delta );
291 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000292}
293
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000294/* It's OK to use a global because alarm() is supposed to be global anyway */
295static DWORD alarmMs;
296
irwire931d0e2018-06-23 18:55:14 +0300297static void TimerProc( void *TimerContext )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100298{
irwire1b82ad2018-08-30 11:57:09 +0300299 (void) TimerContext;
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000300 Sleep( alarmMs );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 mbedtls_timing_alarmed = 1;
irwirda642d92018-08-31 15:14:54 +0300302 /* _endthread will be called implicitly on return
303 * That ensures execution of thread funcition's epilogue */
Paul Bakker5121ce52009-01-03 21:22:43 +0000304}
305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306void mbedtls_set_alarm( int seconds )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100307{
Manuel Pégourié-Gonnard54059622018-01-29 10:16:30 +0100308 if( seconds == 0 )
309 {
310 /* No need to create a thread for this simple case.
311 * Also, this shorcut is more reliable at least on MinGW32 */
312 mbedtls_timing_alarmed = 1;
313 return;
314 }
315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_timing_alarmed = 0;
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000317 alarmMs = seconds * 1000;
irwire1b82ad2018-08-30 11:57:09 +0300318 (void) _beginthread( TimerProc, 0, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000319}
320
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100321#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000324{
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 struct _hr_time *t = (struct _hr_time *) val;
326
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 if( reset )
328 {
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200329 gettimeofday( &t->start, NULL );
Alfred Klompb308dd72014-07-14 22:32:21 +0200330 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 }
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200332 else
333 {
334 unsigned long delta;
335 struct timeval now;
336 gettimeofday( &now, NULL );
337 delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul
338 + ( now.tv_usec - t->start.tv_usec ) / 1000;
339 return( delta );
340 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000341}
342
343static void sighandler( int signum )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100344{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 mbedtls_timing_alarmed = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000346 signal( signum, sighandler );
347}
348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349void mbedtls_set_alarm( int seconds )
Paul Bakker5121ce52009-01-03 21:22:43 +0000350{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000352 signal( SIGALRM, sighandler );
353 alarm( seconds );
Gilles Peskinea0af95f2017-10-10 20:10:46 +0200354 if( seconds == 0 )
355 {
356 /* alarm(0) cancelled any previous pending alarm, but the
357 handler won't fire, so raise the flag straight away. */
358 mbedtls_timing_alarmed = 1;
359 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000360}
361
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100362#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000363
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200364/*
365 * Set delays to watch
366 */
367void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms )
368{
369 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
370
371 ctx->int_ms = int_ms;
372 ctx->fin_ms = fin_ms;
373
374 if( fin_ms != 0 )
375 (void) mbedtls_timing_get_timer( &ctx->timer, 1 );
376}
377
378/*
379 * Get number of delays expired
380 */
381int mbedtls_timing_get_delay( void *data )
382{
383 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
384 unsigned long elapsed_ms;
385
386 if( ctx->fin_ms == 0 )
387 return( -1 );
388
389 elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 );
390
391 if( elapsed_ms >= ctx->fin_ms )
392 return( 2 );
393
394 if( elapsed_ms >= ctx->int_ms )
395 return( 1 );
396
397 return( 0 );
398}
399
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200400#endif /* !MBEDTLS_TIMING_ALT */
401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100403
404/*
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200405 * Busy-waits for the given number of milliseconds.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 * Used for testing mbedtls_timing_hardclock.
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200407 */
408static void busy_msleep( unsigned long msec )
409{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200411 unsigned long i = 0; /* for busy-waiting */
412 volatile unsigned long j; /* to prevent optimisation */
413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 while( mbedtls_timing_get_timer( &hires, 0 ) < msec )
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200417 i++;
418
419 j = i;
420 (void) j;
421}
422
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200423#define FAIL do \
424 { \
425 if( verbose != 0 ) \
426 { \
427 mbedtls_printf( "failed at line %d\n", __LINE__ ); \
428 mbedtls_printf( " cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d a=%lu b=%lu\n", \
429 cycles, ratio, millisecs, secs, hardfail, \
430 (unsigned long) a, (unsigned long) b ); \
431 mbedtls_printf( " elapsed(hires)=%lu elapsed(ctx)=%lu status(ctx)=%d\n", \
432 mbedtls_timing_get_timer( &hires, 0 ), \
433 mbedtls_timing_get_timer( &ctx.timer, 0 ), \
434 mbedtls_timing_get_delay( &ctx ) ); \
435 } \
436 return( 1 ); \
437 } while( 0 )
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200438
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200439/*
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100440 * Checkup routine
Manuel Pégourié-Gonnard0f79bab2014-04-09 09:56:16 +0200441 *
442 * Warning: this is work in progress, some tests may not be reliable enough
443 * yet! False positives may happen.
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100444 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445int mbedtls_timing_self_test( int verbose )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100446{
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200447 unsigned long cycles = 0, ratio = 0;
448 unsigned long millisecs = 0, secs = 0;
449 int hardfail = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 struct mbedtls_timing_hr_time hires;
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200451 uint32_t a = 0, b = 0;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200452 mbedtls_timing_delay_context ctx;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100453
Paul Bakker66d5d072014-06-17 16:39:18 +0200454 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 mbedtls_printf( " TIMING tests note: will take some time!\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100456
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100457 if( verbose != 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200458 mbedtls_printf( " TIMING test #1 (set_alarm / get_timer): " );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100459
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100460 {
Gilles Peskineada3ee82017-12-20 22:31:17 +0100461 secs = 1;
462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_set_alarm( (int) secs );
466 while( !mbedtls_timing_alarmed )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100467 ;
468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 millisecs = mbedtls_timing_get_timer( &hires, 0 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100470
Manuel Pégourié-Gonnarde578b1c2015-08-18 20:11:48 +0200471 /* For some reason on Windows it looks like alarm has an extra delay
472 * (maybe related to creating a new thread). Allow some room here. */
473 if( millisecs < 800 * secs || millisecs > 1200 * secs + 300 )
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200474 FAIL;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100475 }
476
477 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100479
480 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200481 mbedtls_printf( " TIMING test #2 (set/get_delay ): " );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200482
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200483 {
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200484 a = 800;
485 b = 400;
486 mbedtls_timing_set_delay( &ctx, a, a + b ); /* T = 0 */
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200487
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200488 busy_msleep( a - a / 4 ); /* T = a - a/4 */
489 if( mbedtls_timing_get_delay( &ctx ) != 0 )
490 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200491
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200492 busy_msleep( a / 4 + b / 4 ); /* T = a + b/4 */
493 if( mbedtls_timing_get_delay( &ctx ) != 1 )
494 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200495
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200496 busy_msleep( b ); /* T = a + b + b/4 */
497 if( mbedtls_timing_get_delay( &ctx ) != 2 )
498 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200499 }
500
501 mbedtls_timing_set_delay( &ctx, 0, 0 );
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200502 busy_msleep( 200 );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200503 if( mbedtls_timing_get_delay( &ctx ) != -1 )
504 FAIL;
505
506 if( verbose != 0 )
507 mbedtls_printf( "passed\n" );
508
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200509 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200510 mbedtls_printf( " TIMING test #3 (hardclock / get_timer): " );
511
512 /*
513 * Allow one failure for possible counter wrapping.
514 * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
515 * since the whole test is about 10ms, it shouldn't happen twice in a row.
516 */
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200517
518hard_test:
519 if( hardfail > 1 )
520 {
521 if( verbose != 0 )
522 mbedtls_printf( "failed (ignored)\n" );
523
524 goto hard_test_done;
525 }
526
527 /* Get a reference ratio cycles/ms */
528 millisecs = 1;
529 cycles = mbedtls_timing_hardclock();
530 busy_msleep( millisecs );
531 cycles = mbedtls_timing_hardclock() - cycles;
532 ratio = cycles / millisecs;
533
534 /* Check that the ratio is mostly constant */
535 for( millisecs = 2; millisecs <= 4; millisecs++ )
536 {
537 cycles = mbedtls_timing_hardclock();
538 busy_msleep( millisecs );
539 cycles = mbedtls_timing_hardclock() - cycles;
540
541 /* Allow variation up to 20% */
542 if( cycles / millisecs < ratio - ratio / 5 ||
543 cycles / millisecs > ratio + ratio / 5 )
544 {
545 hardfail++;
546 goto hard_test;
547 }
548 }
549
550 if( verbose != 0 )
551 mbedtls_printf( "passed\n" );
552
553hard_test_done:
554
555 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200557
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100558 return( 0 );
559}
560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100562
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200563#endif /* MBEDTLS_TIMING_C */