blob: 4b25057f9a9bf65c972e50c4faccc88657bdb51a [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ú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 * **********
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) && \
69 !defined(__APPLE__) && !defined(_WIN32)
70#error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in config.h"
71#endif
72
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020073#ifndef asm
74#define asm __asm
75#endif
76
Paul Bakkerfa6a6202013-10-28 18:48:30 +010077#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000078
79#include <windows.h>
irwir8efb3cc2018-06-23 18:55:14 +030080#include <process.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000081
82struct _hr_time
83{
84 LARGE_INTEGER start;
85};
86
87#else
88
89#include <unistd.h>
90#include <sys/types.h>
91#include <sys/time.h>
92#include <signal.h>
93#include <time.h>
94
95struct _hr_time
96{
97 struct timeval start;
98};
99
Paul Bakker9af723c2014-05-01 13:03:14 +0200100#endif /* _WIN32 && !EFIX64 && !EFI32 */
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(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__)
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200105#define HAVE_HARDCLOCK
Paul Bakkerbb0139c2012-10-31 09:53:08 +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 tsc;
110 __asm rdtsc
111 __asm mov [tsc], eax
112 return( tsc );
113}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200114#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200115 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +0000117/* some versions of mingw-64 have 32-bit longs even on x84_64 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200118#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Manuel Pégourié-Gonnard38433532015-02-11 11:35:58 +0000119 defined(__GNUC__) && ( defined(__i386__) || ( \
120 ( defined(__amd64__) || defined( __x86_64__) ) && __SIZEOF_LONG__ == 4 ) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000121
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200122#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000125{
Paul Bakkerca410102011-10-19 14:27:36 +0000126 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100127 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakkerca410102011-10-19 14:27:36 +0000128 return( lo );
Paul Bakker5121ce52009-01-03 21:22:43 +0000129}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200130#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200131 __GNUC__ && __i386__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200133#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +0200134 defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000135
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200136#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000139{
140 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100141 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200142 return( lo | ( hi << 32 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200144#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200145 __GNUC__ && ( __amd64__ || __x86_64__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200147#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +0200148 defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000149
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200150#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000153{
154 unsigned long tbl, tbu0, tbu1;
155
156 do
157 {
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100158 asm volatile( "mftbu %0" : "=r" (tbu0) );
159 asm volatile( "mftb %0" : "=r" (tbl ) );
160 asm volatile( "mftbu %0" : "=r" (tbu1) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 }
162 while( tbu0 != tbu1 );
163
164 return( tbl );
165}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200166#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200167 __GNUC__ && ( __powerpc__ || __ppc__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200169#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000170 defined(__GNUC__) && defined(__sparc64__)
171
172#if defined(__OpenBSD__)
173#warning OpenBSD does not allow access to tick register using software version instead
Paul Bakker5121ce52009-01-03 21:22:43 +0000174#else
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200175#define HAVE_HARDCLOCK
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177unsigned long mbedtls_timing_hardclock( void )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000178{
179 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100180 asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) );
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000181 return( tick );
182}
Paul Bakker9af723c2014-05-01 13:03:14 +0200183#endif /* __OpenBSD__ */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200184#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200185 __GNUC__ && __sparc64__ */
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000186
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200187#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000188 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
189
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200190#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000193{
194 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100195 asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" );
196 asm volatile( "mov %%g1, %0" : "=r" (tick) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 return( tick );
198}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200199#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200200 __GNUC__ && __sparc__ && !__sparc64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000201
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200202#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000203 defined(__GNUC__) && defined(__alpha__)
204
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200205#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000208{
209 unsigned long cc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100210 asm volatile( "rpcc %0" : "=r" (cc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 return( cc & 0xFFFFFFFF );
212}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200213#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200214 __GNUC__ && __alpha__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000215
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200216#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000217 defined(__GNUC__) && defined(__ia64__)
218
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200219#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000222{
223 unsigned long itc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100224 asm volatile( "mov %0 = ar.itc" : "=r" (itc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 return( itc );
226}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200227#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
Paul Bakker9af723c2014-05-01 13:03:14 +0200228 __GNUC__ && __ia64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000229
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200230#if !defined(HAVE_HARDCLOCK) && defined(_MSC_VER) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100231 !defined(EFIX64) && !defined(EFI32)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000232
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200233#define HAVE_HARDCLOCK
Paul Bakker2eee9022011-04-24 15:28:55 +0000234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235unsigned long mbedtls_timing_hardclock( void )
Paul Bakker2eee9022011-04-24 15:28:55 +0000236{
237 LARGE_INTEGER offset;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100238
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100239 QueryPerformanceCounter( &offset );
Paul Bakker2eee9022011-04-24 15:28:55 +0000240
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200241 return( (unsigned long)( offset.QuadPart ) );
Paul Bakker2eee9022011-04-24 15:28:55 +0000242}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200243#endif /* !HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
Paul Bakker2eee9022011-04-24 15:28:55 +0000244
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200245#if !defined(HAVE_HARDCLOCK)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000246
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200247#define HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000248
249static int hardclock_init = 0;
250static struct timeval tv_init;
251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252unsigned long mbedtls_timing_hardclock( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000253{
254 struct timeval tv_cur;
255
256 if( hardclock_init == 0 )
257 {
258 gettimeofday( &tv_init, NULL );
259 hardclock_init = 1;
260 }
261
262 gettimeofday( &tv_cur, NULL );
263 return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
264 + ( tv_cur.tv_usec - tv_init.tv_usec ) );
265}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200266#endif /* !HAVE_HARDCLOCK */
Paul Bakker5121ce52009-01-03 21:22:43 +0000267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268volatile int mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000269
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100270#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000273{
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 struct _hr_time *t = (struct _hr_time *) val;
275
Paul Bakker5121ce52009-01-03 21:22:43 +0000276 if( reset )
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200277 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000278 QueryPerformanceCounter( &t->start );
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200279 return( 0 );
280 }
281 else
282 {
283 unsigned long delta;
284 LARGE_INTEGER now, hfreq;
285 QueryPerformanceCounter( &now );
286 QueryPerformanceFrequency( &hfreq );
287 delta = (unsigned long)( ( now.QuadPart - t->start.QuadPart ) * 1000ul
288 / hfreq.QuadPart );
289 return( delta );
290 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000291}
292
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000293/* It's OK to use a global because alarm() is supposed to be global anyway */
294static DWORD alarmMs;
295
irwir8efb3cc2018-06-23 18:55:14 +0300296static void TimerProc( void *TimerContext )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100297{
irwir5afc7ba2018-08-30 11:57:09 +0300298 (void) TimerContext;
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000299 Sleep( alarmMs );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 mbedtls_timing_alarmed = 1;
irwir88eeb4f2018-08-31 15:14:54 +0300301 /* _endthread will be called implicitly on return
302 * That ensures execution of thread funcition's epilogue */
Paul Bakker5121ce52009-01-03 21:22:43 +0000303}
304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305void mbedtls_set_alarm( int seconds )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100306{
Manuel Pégourié-Gonnard54059622018-01-29 10:16:30 +0100307 if( seconds == 0 )
308 {
309 /* No need to create a thread for this simple case.
310 * Also, this shorcut is more reliable at least on MinGW32 */
311 mbedtls_timing_alarmed = 1;
312 return;
313 }
314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 mbedtls_timing_alarmed = 0;
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +0000316 alarmMs = seconds * 1000;
irwir5afc7ba2018-08-30 11:57:09 +0300317 (void) _beginthread( TimerProc, 0, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000318}
319
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100320#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
Paul Bakker5121ce52009-01-03 21:22:43 +0000323{
Paul Bakker5121ce52009-01-03 21:22:43 +0000324 struct _hr_time *t = (struct _hr_time *) val;
325
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 if( reset )
327 {
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200328 gettimeofday( &t->start, NULL );
Alfred Klompb308dd72014-07-14 22:32:21 +0200329 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 }
Gilles Peskined92f0aa2017-10-16 19:33:06 +0200331 else
332 {
333 unsigned long delta;
334 struct timeval now;
335 gettimeofday( &now, NULL );
336 delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul
337 + ( now.tv_usec - t->start.tv_usec ) / 1000;
338 return( delta );
339 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000340}
341
342static void sighandler( int signum )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100343{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 mbedtls_timing_alarmed = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000345 signal( signum, sighandler );
346}
347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348void mbedtls_set_alarm( int seconds )
Paul Bakker5121ce52009-01-03 21:22:43 +0000349{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 mbedtls_timing_alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000351 signal( SIGALRM, sighandler );
352 alarm( seconds );
Gilles Peskinea0af95f2017-10-10 20:10:46 +0200353 if( seconds == 0 )
354 {
355 /* alarm(0) cancelled any previous pending alarm, but the
356 handler won't fire, so raise the flag straight away. */
357 mbedtls_timing_alarmed = 1;
358 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000359}
360
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100361#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000362
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200363/*
364 * Set delays to watch
365 */
366void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms )
367{
368 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
369
370 ctx->int_ms = int_ms;
371 ctx->fin_ms = fin_ms;
372
373 if( fin_ms != 0 )
374 (void) mbedtls_timing_get_timer( &ctx->timer, 1 );
375}
376
377/*
378 * Get number of delays expired
379 */
380int mbedtls_timing_get_delay( void *data )
381{
382 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
383 unsigned long elapsed_ms;
384
385 if( ctx->fin_ms == 0 )
386 return( -1 );
387
388 elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 );
389
390 if( elapsed_ms >= ctx->fin_ms )
391 return( 2 );
392
393 if( elapsed_ms >= ctx->int_ms )
394 return( 1 );
395
396 return( 0 );
397}
398
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200399#endif /* !MBEDTLS_TIMING_ALT */
400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100402
403/*
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200404 * Busy-waits for the given number of milliseconds.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 * Used for testing mbedtls_timing_hardclock.
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200406 */
407static void busy_msleep( unsigned long msec )
408{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200410 unsigned long i = 0; /* for busy-waiting */
411 volatile unsigned long j; /* to prevent optimisation */
412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 while( mbedtls_timing_get_timer( &hires, 0 ) < msec )
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200416 i++;
417
418 j = i;
419 (void) j;
420}
421
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200422#define FAIL do \
423 { \
424 if( verbose != 0 ) \
425 { \
426 mbedtls_printf( "failed at line %d\n", __LINE__ ); \
427 mbedtls_printf( " cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d a=%lu b=%lu\n", \
428 cycles, ratio, millisecs, secs, hardfail, \
429 (unsigned long) a, (unsigned long) b ); \
430 mbedtls_printf( " elapsed(hires)=%lu elapsed(ctx)=%lu status(ctx)=%d\n", \
431 mbedtls_timing_get_timer( &hires, 0 ), \
432 mbedtls_timing_get_timer( &ctx.timer, 0 ), \
433 mbedtls_timing_get_delay( &ctx ) ); \
434 } \
435 return( 1 ); \
436 } while( 0 )
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200437
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200438/*
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100439 * Checkup routine
Manuel Pégourié-Gonnard0f79bab2014-04-09 09:56:16 +0200440 *
441 * Warning: this is work in progress, some tests may not be reliable enough
442 * yet! False positives may happen.
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100443 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444int mbedtls_timing_self_test( int verbose )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100445{
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200446 unsigned long cycles = 0, ratio = 0;
447 unsigned long millisecs = 0, secs = 0;
448 int hardfail = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 struct mbedtls_timing_hr_time hires;
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200450 uint32_t a = 0, b = 0;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200451 mbedtls_timing_delay_context ctx;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100452
Paul Bakker66d5d072014-06-17 16:39:18 +0200453 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_printf( " TIMING tests note: will take some time!\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100455
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100456 if( verbose != 0 )
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200457 mbedtls_printf( " TIMING test #1 (set_alarm / get_timer): " );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100458
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100459 {
Gilles Peskineada3ee82017-12-20 22:31:17 +0100460 secs = 1;
461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 (void) mbedtls_timing_get_timer( &hires, 1 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_set_alarm( (int) secs );
465 while( !mbedtls_timing_alarmed )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100466 ;
467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 millisecs = mbedtls_timing_get_timer( &hires, 0 );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100469
Manuel Pégourié-Gonnarde578b1c2015-08-18 20:11:48 +0200470 /* For some reason on Windows it looks like alarm has an extra delay
471 * (maybe related to creating a new thread). Allow some room here. */
472 if( millisecs < 800 * secs || millisecs > 1200 * secs + 300 )
Gilles Peskine0827d5c2017-10-10 20:09:26 +0200473 FAIL;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100474 }
475
476 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100478
479 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200480 mbedtls_printf( " TIMING test #2 (set/get_delay ): " );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200481
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200482 {
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200483 a = 800;
484 b = 400;
485 mbedtls_timing_set_delay( &ctx, a, a + b ); /* T = 0 */
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200486
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200487 busy_msleep( a - a / 4 ); /* T = a - a/4 */
488 if( mbedtls_timing_get_delay( &ctx ) != 0 )
489 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200490
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200491 busy_msleep( a / 4 + b / 4 ); /* T = a + b/4 */
492 if( mbedtls_timing_get_delay( &ctx ) != 1 )
493 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200494
Gilles Peskine8873bcc2017-10-27 18:42:32 +0200495 busy_msleep( b ); /* T = a + b + b/4 */
496 if( mbedtls_timing_get_delay( &ctx ) != 2 )
497 FAIL;
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200498 }
499
500 mbedtls_timing_set_delay( &ctx, 0, 0 );
Manuel Pégourié-Gonnarda63bc942015-05-14 18:22:47 +0200501 busy_msleep( 200 );
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +0200502 if( mbedtls_timing_get_delay( &ctx ) != -1 )
503 FAIL;
504
505 if( verbose != 0 )
506 mbedtls_printf( "passed\n" );
507
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200508 if( verbose != 0 )
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200509 mbedtls_printf( " TIMING test #3 (hardclock / get_timer): " );
510
511 /*
512 * Allow one failure for possible counter wrapping.
513 * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
514 * since the whole test is about 10ms, it shouldn't happen twice in a row.
515 */
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +0200516
517hard_test:
518 if( hardfail > 1 )
519 {
520 if( verbose != 0 )
521 mbedtls_printf( "failed (ignored)\n" );
522
523 goto hard_test_done;
524 }
525
526 /* Get a reference ratio cycles/ms */
527 millisecs = 1;
528 cycles = mbedtls_timing_hardclock();
529 busy_msleep( millisecs );
530 cycles = mbedtls_timing_hardclock() - cycles;
531 ratio = cycles / millisecs;
532
533 /* Check that the ratio is mostly constant */
534 for( millisecs = 2; millisecs <= 4; millisecs++ )
535 {
536 cycles = mbedtls_timing_hardclock();
537 busy_msleep( millisecs );
538 cycles = mbedtls_timing_hardclock() - cycles;
539
540 /* Allow variation up to 20% */
541 if( cycles / millisecs < ratio - ratio / 5 ||
542 cycles / millisecs > ratio + ratio / 5 )
543 {
544 hardfail++;
545 goto hard_test;
546 }
547 }
548
549 if( verbose != 0 )
550 mbedtls_printf( "passed\n" );
551
552hard_test_done:
553
554 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200556
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100557 return( 0 );
558}
559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100561
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200562#endif /* MBEDTLS_TIMING_C */