blob: 6b91ea43c7e7eed23302c30a0c19476ad17e0d22 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Portable interface to the CPU cycle counter
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-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úti4e9f7122020-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 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010055#else
56#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define mbedtls_printf printf
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010058#endif
59
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +020060#if defined(MBEDTLS_TIMING_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000063
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +020064#if !defined(MBEDTLS_TIMING_ALT)
65
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010066#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
67 !defined(__APPLE__) && !defined(_WIN32)
68#error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in config.h"
69#endif
70
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020071#ifndef asm
72#define asm __asm
73#endif
74
Paul Bakkerfa6a6202013-10-28 18:48:30 +010075#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000076
77#include <windows.h>
irwir8efb3cc2018-06-23 18:55:14 +030078#include <process.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000079
80struct _hr_time
81{
82 LARGE_INTEGER start;
83};
84
85#else
86
87#include <unistd.h>
88#include <sys/types.h>
89#include <sys/time.h>
90#include <signal.h>
91#include <time.h>
92
93struct _hr_time
94{
95 struct timeval start;
96};
97
Paul Bakker9af723c2014-05-01 13:03:14 +020098#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000099
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200100#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +0200101 ( defined(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__)
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200103#define HAVE_HARDCLOCK
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000106{
107 unsigned long tsc;
108 __asm rdtsc
109 __asm mov [tsc], eax
110 return( tsc );
111}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200112#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200113 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +0000115/* some versions of mingw-64 have 32-bit longs even on x84_64 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200116#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +0000117 defined(__GNUC__) && ( defined(__i386__) || ( \
118 ( defined(__amd64__) || defined( __x86_64__) ) && __SIZEOF_LONG__ == 4 ) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000119
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200120#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000123{
Paul Bakkerca410102011-10-19 14:27:36 +0000124 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100125 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakkerca410102011-10-19 14:27:36 +0000126 return( lo );
Paul Bakker5121ce52009-01-03 21:22:43 +0000127}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200128#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200129 __GNUC__ && __i386__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200131#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +0200132 defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000133
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200134#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000137{
138 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100139 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200140 return( lo | ( hi << 32 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200142#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200143 __GNUC__ && ( __amd64__ || __x86_64__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200145#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +0200146 defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000147
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200148#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000151{
152 unsigned long tbl, tbu0, tbu1;
153
154 do
155 {
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100156 asm volatile( "mftbu %0" : "=r" (tbu0) );
157 asm volatile( "mftb %0" : "=r" (tbl ) );
158 asm volatile( "mftbu %0" : "=r" (tbu1) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 }
160 while( tbu0 != tbu1 );
161
162 return( tbl );
163}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200164#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200165 __GNUC__ && ( __powerpc__ || __ppc__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200167#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000168 defined(__GNUC__) && defined(__sparc64__)
169
170#if defined(__OpenBSD__)
171#warning OpenBSD does not allow access to tick register using software version instead
Paul Bakker5121ce52009-01-03 21:22:43 +0000172#else
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200173#define HAVE_HARDCLOCK
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175unsigned long mbedtls_timing_hardclock( void )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000176{
177 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100178 asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) );
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000179 return( tick );
180}
Paul Bakker9af723c2014-05-01 13:03:14 +0200181#endif /* __OpenBSD__ */
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__ && __sparc64__ */
Paul Bakkerbb0139c2012-10-31 09:53:08 +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(__sparc__) && !defined(__sparc64__)
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 tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100193 asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" );
194 asm volatile( "mov %%g1, %0" : "=r" (tick) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000195 return( tick );
196}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200197#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200198 __GNUC__ && __sparc__ && !__sparc64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000199
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200200#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000201 defined(__GNUC__) && defined(__alpha__)
202
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200203#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000206{
207 unsigned long cc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100208 asm volatile( "rpcc %0" : "=r" (cc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 return( cc & 0xFFFFFFFF );
210}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200211#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200212 __GNUC__ && __alpha__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200214#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000215 defined(__GNUC__) && defined(__ia64__)
216
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200217#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000220{
221 unsigned long itc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100222 asm volatile( "mov %0 = ar.itc" : "=r" (itc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 return( itc );
224}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200225#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200226 __GNUC__ && __ia64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000227
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200228#if !defined(HAVE_HARDCLOCK) && defined(_MSC_VER) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100229 !defined(EFIX64) && !defined(EFI32)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000230
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200231#define HAVE_HARDCLOCK
Paul Bakker2eee9022011-04-24 15:28:55 +0000232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233unsigned long mbedtls_timing_hardclock( void )
Paul Bakker2eee9022011-04-24 15:28:55 +0000234{
235 LARGE_INTEGER offset;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100236
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100237 QueryPerformanceCounter( &offset );
Paul Bakker2eee9022011-04-24 15:28:55 +0000238
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200239 return( (unsigned long)( offset.QuadPart ) );
Paul Bakker2eee9022011-04-24 15:28:55 +0000240}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200241#endif /* !HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
Paul Bakker2eee9022011-04-24 15:28:55 +0000242
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200243#if !defined(HAVE_HARDCLOCK)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000244
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200245#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000246
247static int hardclock_init = 0;
248static struct timeval tv_init;
249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000251{
252 struct timeval tv_cur;
253
254 if( hardclock_init == 0 )
255 {
256 gettimeofday( &tv_init, NULL );
257 hardclock_init = 1;
258 }
259
260 gettimeofday( &tv_cur, NULL );
261 return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
262 + ( tv_cur.tv_usec - tv_init.tv_usec ) );
263}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200264#endif /* !HAVE_HARDCLOCK */
Paul Bakker5121ce52009-01-03 21:22:43 +0000265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266volatile int mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000267
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100268#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000271{
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 struct _hr_time *t = (struct _hr_time *) val;
273
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 if( reset )
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200275 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000276 QueryPerformanceCounter( &t->start );
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200277 return( 0 );
278 }
279 else
280 {
281 unsigned long delta;
282 LARGE_INTEGER now, hfreq;
283 QueryPerformanceCounter( &now );
284 QueryPerformanceFrequency( &hfreq );
285 delta = (unsigned long)( ( now.QuadPart - t->start.QuadPart ) * 1000ul
286 / hfreq.QuadPart );
287 return( delta );
288 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000289}
290
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000291/* It's OK to use a global because alarm() is supposed to be global anyway */
292static DWORD alarmMs;
293
irwir8efb3cc2018-06-23 18:55:14 +0300294static void TimerProc( void *TimerContext )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100295{
irwir5afc7ba2018-08-30 11:57:09 +0300296 (void) TimerContext;
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000297 Sleep( alarmMs );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_timing_alarmed = 1;
irwir88eeb4f2018-08-31 15:14:54 +0300299 /* _endthread will be called implicitly on return
300 * That ensures execution of thread funcition's epilogue */
Paul Bakker5121ce52009-01-03 21:22:43 +0000301}
302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303void mbedtls_set_alarm( int seconds )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100304{
Manuel Pégourié-Gonnard54059622018-01-29 10:16:30 +0100305 if( seconds == 0 )
306 {
307 /* No need to create a thread for this simple case.
308 * Also, this shorcut is more reliable at least on MinGW32 */
309 mbedtls_timing_alarmed = 1;
310 return;
311 }
312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_timing_alarmed = 0;
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000314 alarmMs = seconds * 1000;
irwir5afc7ba2018-08-30 11:57:09 +0300315 (void) _beginthread( TimerProc, 0, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000316}
317
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100318#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000321{
Paul Bakker5121ce52009-01-03 21:22:43 +0000322 struct _hr_time *t = (struct _hr_time *) val;
323
Paul Bakker5121ce52009-01-03 21:22:43 +0000324 if( reset )
325 {
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200326 gettimeofday( &t->start, NULL );
Alfred Klompb308dd72014-07-14 22:32:21 +0200327 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000328 }
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200329 else
330 {
331 unsigned long delta;
332 struct timeval now;
333 gettimeofday( &now, NULL );
334 delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul
335 + ( now.tv_usec - t->start.tv_usec ) / 1000;
336 return( delta );
337 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000338}
339
340static void sighandler( int signum )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100341{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 mbedtls_timing_alarmed = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000343 signal( signum, sighandler );
344}
345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346void mbedtls_set_alarm( int seconds )
Paul Bakker5121ce52009-01-03 21:22:43 +0000347{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000349 signal( SIGALRM, sighandler );
350 alarm( seconds );
Gilles Peskinea0af95f2017-10-10 20:10:46 +0200351 if( seconds == 0 )
352 {
353 /* alarm(0) cancelled any previous pending alarm, but the
354 handler won't fire, so raise the flag straight away. */
355 mbedtls_timing_alarmed = 1;
356 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000357}
358
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100359#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000360
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200361/*
362 * Set delays to watch
363 */
364void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms )
365{
366 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
367
368 ctx->int_ms = int_ms;
369 ctx->fin_ms = fin_ms;
370
371 if( fin_ms != 0 )
372 (void) mbedtls_timing_get_timer( &ctx->timer, 1 );
373}
374
375/*
376 * Get number of delays expired
377 */
378int mbedtls_timing_get_delay( void *data )
379{
380 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
381 unsigned long elapsed_ms;
382
383 if( ctx->fin_ms == 0 )
384 return( -1 );
385
386 elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 );
387
388 if( elapsed_ms >= ctx->fin_ms )
389 return( 2 );
390
391 if( elapsed_ms >= ctx->int_ms )
392 return( 1 );
393
394 return( 0 );
395}
396
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200397#endif /* !MBEDTLS_TIMING_ALT */
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100400
401/*
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200402 * Busy-waits for the given number of milliseconds.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 * Used for testing mbedtls_timing_hardclock.
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200404 */
405static void busy_msleep( unsigned long msec )
406{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200408 unsigned long i = 0; /* for busy-waiting */
409 volatile unsigned long j; /* to prevent optimisation */
410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 while( mbedtls_timing_get_timer( &hires, 0 ) < msec )
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200414 i++;
415
416 j = i;
417 (void) j;
418}
419
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200420#define FAIL do \
421 { \
422 if( verbose != 0 ) \
423 { \
424 mbedtls_printf( "failed at line %d\n", __LINE__ ); \
425 mbedtls_printf( " cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d a=%lu b=%lu\n", \
426 cycles, ratio, millisecs, secs, hardfail, \
427 (unsigned long) a, (unsigned long) b ); \
428 mbedtls_printf( " elapsed(hires)=%lu elapsed(ctx)=%lu status(ctx)=%d\n", \
429 mbedtls_timing_get_timer( &hires, 0 ), \
430 mbedtls_timing_get_timer( &ctx.timer, 0 ), \
431 mbedtls_timing_get_delay( &ctx ) ); \
432 } \
433 return( 1 ); \
434 } while( 0 )
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200435
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200436/*
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100437 * Checkup routine
Manuel Pégourié-Gonnard0f79bab2014-04-09 09:56:16 +0200438 *
439 * Warning: this is work in progress, some tests may not be reliable enough
440 * yet! False positives may happen.
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442int mbedtls_timing_self_test( int verbose )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100443{
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200444 unsigned long cycles = 0, ratio = 0;
445 unsigned long millisecs = 0, secs = 0;
446 int hardfail = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 struct mbedtls_timing_hr_time hires;
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200448 uint32_t a = 0, b = 0;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200449 mbedtls_timing_delay_context ctx;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100450
Paul Bakker66d5d072014-06-17 16:39:18 +0200451 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_printf( " TIMING tests note: will take some time!\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100453
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100454 if( verbose != 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200455 mbedtls_printf( " TIMING test #1 (set_alarm / get_timer): " );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100456
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100457 {
Gilles Peskineada3ee82017-12-20 22:31:17 +0100458 secs = 1;
459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_set_alarm( (int) secs );
463 while( !mbedtls_timing_alarmed )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100464 ;
465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 millisecs = mbedtls_timing_get_timer( &hires, 0 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100467
Manuel Pégourié-Gonnarde578b1c2015-08-18 20:11:48 +0200468 /* For some reason on Windows it looks like alarm has an extra delay
469 * (maybe related to creating a new thread). Allow some room here. */
470 if( millisecs < 800 * secs || millisecs > 1200 * secs + 300 )
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200471 FAIL;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100472 }
473
474 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100476
477 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200478 mbedtls_printf( " TIMING test #2 (set/get_delay ): " );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200479
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200480 {
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200481 a = 800;
482 b = 400;
483 mbedtls_timing_set_delay( &ctx, a, a + b ); /* T = 0 */
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200484
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200485 busy_msleep( a - a / 4 ); /* T = a - a/4 */
486 if( mbedtls_timing_get_delay( &ctx ) != 0 )
487 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200488
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200489 busy_msleep( a / 4 + b / 4 ); /* T = a + b/4 */
490 if( mbedtls_timing_get_delay( &ctx ) != 1 )
491 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200492
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200493 busy_msleep( b ); /* T = a + b + b/4 */
494 if( mbedtls_timing_get_delay( &ctx ) != 2 )
495 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200496 }
497
498 mbedtls_timing_set_delay( &ctx, 0, 0 );
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200499 busy_msleep( 200 );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200500 if( mbedtls_timing_get_delay( &ctx ) != -1 )
501 FAIL;
502
503 if( verbose != 0 )
504 mbedtls_printf( "passed\n" );
505
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200506 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200507 mbedtls_printf( " TIMING test #3 (hardclock / get_timer): " );
508
509 /*
510 * Allow one failure for possible counter wrapping.
511 * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
512 * since the whole test is about 10ms, it shouldn't happen twice in a row.
513 */
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200514
515hard_test:
516 if( hardfail > 1 )
517 {
518 if( verbose != 0 )
519 mbedtls_printf( "failed (ignored)\n" );
520
521 goto hard_test_done;
522 }
523
524 /* Get a reference ratio cycles/ms */
525 millisecs = 1;
526 cycles = mbedtls_timing_hardclock();
527 busy_msleep( millisecs );
528 cycles = mbedtls_timing_hardclock() - cycles;
529 ratio = cycles / millisecs;
530
531 /* Check that the ratio is mostly constant */
532 for( millisecs = 2; millisecs <= 4; millisecs++ )
533 {
534 cycles = mbedtls_timing_hardclock();
535 busy_msleep( millisecs );
536 cycles = mbedtls_timing_hardclock() - cycles;
537
538 /* Allow variation up to 20% */
539 if( cycles / millisecs < ratio - ratio / 5 ||
540 cycles / millisecs > ratio + ratio / 5 )
541 {
542 hardfail++;
543 goto hard_test;
544 }
545 }
546
547 if( verbose != 0 )
548 mbedtls_printf( "passed\n" );
549
550hard_test_done:
551
552 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200554
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100555 return( 0 );
556}
557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100559
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200560#endif /* MBEDTLS_TIMING_C */