blob: 57bc9bcc12f095d974d0c568605466a3b5217451 [file] [log] [blame]
Jens Wiklander817466c2018-05-22 13:49:31 +02001/*
2 * Portable interface to the CPU cycle counter
3 *
Jerome Forissier79013242021-07-28 10:24:04 +02004 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
Jens Wiklander817466c2018-05-22 13:49:31 +02006 *
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.
Jens Wiklander817466c2018-05-22 13:49:31 +020018 */
19
Jerome Forissier79013242021-07-28 10:24:04 +020020#include "common.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020021
22#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_PLATFORM_C)
23#include "mbedtls/platform.h"
24#else
25#include <stdio.h>
26#define mbedtls_printf printf
27#endif
28
29#if defined(MBEDTLS_TIMING_C)
30
31#include "mbedtls/timing.h"
32
33#if !defined(MBEDTLS_TIMING_ALT)
34
35#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Jens Wiklander3d3b0592019-03-20 15:30:29 +010036 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
Jerome Forissier79013242021-07-28 10:24:04 +020037 !defined(__HAIKU__) && !defined(__midipix__)
Jens Wiklander817466c2018-05-22 13:49:31 +020038#error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in config.h"
39#endif
40
41#ifndef asm
42#define asm __asm
43#endif
44
45#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
46
47#include <windows.h>
Jens Wiklander3d3b0592019-03-20 15:30:29 +010048#include <process.h>
Jens Wiklander817466c2018-05-22 13:49:31 +020049
50struct _hr_time
51{
52 LARGE_INTEGER start;
53};
54
55#else
56
57#include <unistd.h>
58#include <sys/types.h>
Jens Wiklander817466c2018-05-22 13:49:31 +020059#include <signal.h>
Jerome Forissier039e02d2022-08-09 17:10:15 +020060/* time.h should be included independently of MBEDTLS_HAVE_TIME. If the
61 * platform matches the ifdefs above, it will be used. */
Jens Wiklander817466c2018-05-22 13:49:31 +020062#include <time.h>
Jerome Forissier039e02d2022-08-09 17:10:15 +020063#include <sys/time.h>
Jens Wiklander817466c2018-05-22 13:49:31 +020064struct _hr_time
65{
66 struct timeval start;
67};
Jens Wiklander817466c2018-05-22 13:49:31 +020068#endif /* _WIN32 && !EFIX64 && !EFI32 */
69
70#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
71 ( defined(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__)
72
73#define HAVE_HARDCLOCK
74
75unsigned long mbedtls_timing_hardclock( void )
76{
77 unsigned long tsc;
78 __asm rdtsc
79 __asm mov [tsc], eax
80 return( tsc );
81}
82#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
83 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
84
85/* some versions of mingw-64 have 32-bit longs even on x84_64 */
86#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
87 defined(__GNUC__) && ( defined(__i386__) || ( \
88 ( defined(__amd64__) || defined( __x86_64__) ) && __SIZEOF_LONG__ == 4 ) )
89
90#define HAVE_HARDCLOCK
91
92unsigned long mbedtls_timing_hardclock( void )
93{
94 unsigned long lo, hi;
95 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
96 return( lo );
97}
98#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
99 __GNUC__ && __i386__ */
100
101#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
102 defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) )
103
104#define HAVE_HARDCLOCK
105
106unsigned long mbedtls_timing_hardclock( void )
107{
108 unsigned long lo, hi;
109 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
110 return( lo | ( hi << 32 ) );
111}
112#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
113 __GNUC__ && ( __amd64__ || __x86_64__ ) */
114
115#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
116 defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) )
117
118#define HAVE_HARDCLOCK
119
120unsigned long mbedtls_timing_hardclock( void )
121{
122 unsigned long tbl, tbu0, tbu1;
123
124 do
125 {
126 asm volatile( "mftbu %0" : "=r" (tbu0) );
127 asm volatile( "mftb %0" : "=r" (tbl ) );
128 asm volatile( "mftbu %0" : "=r" (tbu1) );
129 }
130 while( tbu0 != tbu1 );
131
132 return( tbl );
133}
134#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
135 __GNUC__ && ( __powerpc__ || __ppc__ ) */
136
137#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
138 defined(__GNUC__) && defined(__sparc64__)
139
140#if defined(__OpenBSD__)
141#warning OpenBSD does not allow access to tick register using software version instead
142#else
143#define HAVE_HARDCLOCK
144
145unsigned long mbedtls_timing_hardclock( void )
146{
147 unsigned long tick;
148 asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) );
149 return( tick );
150}
151#endif /* __OpenBSD__ */
152#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
153 __GNUC__ && __sparc64__ */
154
155#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
156 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
157
158#define HAVE_HARDCLOCK
159
160unsigned long mbedtls_timing_hardclock( void )
161{
162 unsigned long tick;
163 asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" );
164 asm volatile( "mov %%g1, %0" : "=r" (tick) );
165 return( tick );
166}
167#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
168 __GNUC__ && __sparc__ && !__sparc64__ */
169
170#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
171 defined(__GNUC__) && defined(__alpha__)
172
173#define HAVE_HARDCLOCK
174
175unsigned long mbedtls_timing_hardclock( void )
176{
177 unsigned long cc;
178 asm volatile( "rpcc %0" : "=r" (cc) );
179 return( cc & 0xFFFFFFFF );
180}
181#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
182 __GNUC__ && __alpha__ */
183
184#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
185 defined(__GNUC__) && defined(__ia64__)
186
187#define HAVE_HARDCLOCK
188
189unsigned long mbedtls_timing_hardclock( void )
190{
191 unsigned long itc;
192 asm volatile( "mov %0 = ar.itc" : "=r" (itc) );
193 return( itc );
194}
195#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
196 __GNUC__ && __ia64__ */
197
198#if !defined(HAVE_HARDCLOCK) && defined(_MSC_VER) && \
199 !defined(EFIX64) && !defined(EFI32)
200
201#define HAVE_HARDCLOCK
202
203unsigned long mbedtls_timing_hardclock( void )
204{
205 LARGE_INTEGER offset;
206
207 QueryPerformanceCounter( &offset );
208
209 return( (unsigned long)( offset.QuadPart ) );
210}
211#endif /* !HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
212
213#if !defined(HAVE_HARDCLOCK)
214
215#define HAVE_HARDCLOCK
216
217static int hardclock_init = 0;
218static struct timeval tv_init;
219
220unsigned long mbedtls_timing_hardclock( void )
221{
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}
234#endif /* !HAVE_HARDCLOCK */
235
236volatile int mbedtls_timing_alarmed = 0;
237
238#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
239
240unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
241{
Jens Wiklander817466c2018-05-22 13:49:31 +0200242 struct _hr_time *t = (struct _hr_time *) val;
243
Jens Wiklander817466c2018-05-22 13:49:31 +0200244 if( reset )
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100245 {
Jens Wiklander817466c2018-05-22 13:49:31 +0200246 QueryPerformanceCounter( &t->start );
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100247 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 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200259}
260
261/* It's OK to use a global because alarm() is supposed to be global anyway */
262static DWORD alarmMs;
263
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100264static void TimerProc( void *TimerContext )
Jens Wiklander817466c2018-05-22 13:49:31 +0200265{
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100266 (void) TimerContext;
Jens Wiklander817466c2018-05-22 13:49:31 +0200267 Sleep( alarmMs );
268 mbedtls_timing_alarmed = 1;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100269 /* _endthread will be called implicitly on return
270 * That ensures execution of thread funcition's epilogue */
Jens Wiklander817466c2018-05-22 13:49:31 +0200271}
272
273void mbedtls_set_alarm( int seconds )
274{
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100275 if( seconds == 0 )
276 {
277 /* No need to create a thread for this simple case.
278 * Also, this shorcut is more reliable at least on MinGW32 */
279 mbedtls_timing_alarmed = 1;
280 return;
281 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200282
283 mbedtls_timing_alarmed = 0;
284 alarmMs = seconds * 1000;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100285 (void) _beginthread( TimerProc, 0, NULL );
Jens Wiklander817466c2018-05-22 13:49:31 +0200286}
287
288#else /* _WIN32 && !EFIX64 && !EFI32 */
289
290unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
291{
Jens Wiklander817466c2018-05-22 13:49:31 +0200292 struct _hr_time *t = (struct _hr_time *) val;
293
Jens Wiklander817466c2018-05-22 13:49:31 +0200294 if( reset )
295 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100296 gettimeofday( &t->start, NULL );
Jens Wiklander817466c2018-05-22 13:49:31 +0200297 return( 0 );
298 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100299 else
300 {
301 unsigned long delta;
302 struct timeval now;
303 gettimeofday( &now, NULL );
304 delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul
305 + ( now.tv_usec - t->start.tv_usec ) / 1000;
306 return( delta );
307 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200308}
309
310static void sighandler( int signum )
311{
312 mbedtls_timing_alarmed = 1;
313 signal( signum, sighandler );
314}
315
316void mbedtls_set_alarm( int seconds )
317{
318 mbedtls_timing_alarmed = 0;
319 signal( SIGALRM, sighandler );
320 alarm( seconds );
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100321 if( seconds == 0 )
322 {
323 /* alarm(0) cancelled any previous pending alarm, but the
324 handler won't fire, so raise the flag straight away. */
325 mbedtls_timing_alarmed = 1;
326 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200327}
328
329#endif /* _WIN32 && !EFIX64 && !EFI32 */
330
331/*
332 * Set delays to watch
333 */
334void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms )
335{
336 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
337
338 ctx->int_ms = int_ms;
339 ctx->fin_ms = fin_ms;
340
341 if( fin_ms != 0 )
342 (void) mbedtls_timing_get_timer( &ctx->timer, 1 );
343}
344
345/*
346 * Get number of delays expired
347 */
348int mbedtls_timing_get_delay( void *data )
349{
350 mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
351 unsigned long elapsed_ms;
352
353 if( ctx->fin_ms == 0 )
354 return( -1 );
355
356 elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 );
357
358 if( elapsed_ms >= ctx->fin_ms )
359 return( 2 );
360
361 if( elapsed_ms >= ctx->int_ms )
362 return( 1 );
363
364 return( 0 );
365}
366
Jens Wiklander817466c2018-05-22 13:49:31 +0200367
368#if defined(MBEDTLS_SELF_TEST)
369
370/*
371 * Busy-waits for the given number of milliseconds.
372 * Used for testing mbedtls_timing_hardclock.
373 */
374static void busy_msleep( unsigned long msec )
375{
376 struct mbedtls_timing_hr_time hires;
377 unsigned long i = 0; /* for busy-waiting */
378 volatile unsigned long j; /* to prevent optimisation */
379
380 (void) mbedtls_timing_get_timer( &hires, 1 );
381
382 while( mbedtls_timing_get_timer( &hires, 0 ) < msec )
383 i++;
384
385 j = i;
386 (void) j;
387}
388
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100389#define FAIL do \
390 { \
391 if( verbose != 0 ) \
392 { \
393 mbedtls_printf( "failed at line %d\n", __LINE__ ); \
394 mbedtls_printf( " cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d a=%lu b=%lu\n", \
395 cycles, ratio, millisecs, secs, hardfail, \
396 (unsigned long) a, (unsigned long) b ); \
397 mbedtls_printf( " elapsed(hires)=%lu elapsed(ctx)=%lu status(ctx)=%d\n", \
398 mbedtls_timing_get_timer( &hires, 0 ), \
399 mbedtls_timing_get_timer( &ctx.timer, 0 ), \
400 mbedtls_timing_get_delay( &ctx ) ); \
401 } \
402 return( 1 ); \
403 } while( 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +0200404
405/*
406 * Checkup routine
407 *
408 * Warning: this is work in progress, some tests may not be reliable enough
409 * yet! False positives may happen.
410 */
411int mbedtls_timing_self_test( int verbose )
412{
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100413 unsigned long cycles = 0, ratio = 0;
414 unsigned long millisecs = 0, secs = 0;
415 int hardfail = 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200416 struct mbedtls_timing_hr_time hires;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100417 uint32_t a = 0, b = 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200418 mbedtls_timing_delay_context ctx;
419
420 if( verbose != 0 )
421 mbedtls_printf( " TIMING tests note: will take some time!\n" );
422
Jens Wiklander817466c2018-05-22 13:49:31 +0200423 if( verbose != 0 )
424 mbedtls_printf( " TIMING test #1 (set_alarm / get_timer): " );
425
Jens Wiklander817466c2018-05-22 13:49:31 +0200426 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100427 secs = 1;
428
Jens Wiklander817466c2018-05-22 13:49:31 +0200429 (void) mbedtls_timing_get_timer( &hires, 1 );
430
431 mbedtls_set_alarm( (int) secs );
432 while( !mbedtls_timing_alarmed )
433 ;
434
435 millisecs = mbedtls_timing_get_timer( &hires, 0 );
436
437 /* For some reason on Windows it looks like alarm has an extra delay
438 * (maybe related to creating a new thread). Allow some room here. */
439 if( millisecs < 800 * secs || millisecs > 1200 * secs + 300 )
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100440 FAIL;
Jens Wiklander817466c2018-05-22 13:49:31 +0200441 }
442
443 if( verbose != 0 )
444 mbedtls_printf( "passed\n" );
445
446 if( verbose != 0 )
447 mbedtls_printf( " TIMING test #2 (set/get_delay ): " );
448
Jens Wiklander817466c2018-05-22 13:49:31 +0200449 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100450 a = 800;
451 b = 400;
452 mbedtls_timing_set_delay( &ctx, a, a + b ); /* T = 0 */
Jens Wiklander817466c2018-05-22 13:49:31 +0200453
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100454 busy_msleep( a - a / 4 ); /* T = a - a/4 */
455 if( mbedtls_timing_get_delay( &ctx ) != 0 )
456 FAIL;
Jens Wiklander817466c2018-05-22 13:49:31 +0200457
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100458 busy_msleep( a / 4 + b / 4 ); /* T = a + b/4 */
459 if( mbedtls_timing_get_delay( &ctx ) != 1 )
460 FAIL;
Jens Wiklander817466c2018-05-22 13:49:31 +0200461
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100462 busy_msleep( b ); /* T = a + b + b/4 */
463 if( mbedtls_timing_get_delay( &ctx ) != 2 )
464 FAIL;
Jens Wiklander817466c2018-05-22 13:49:31 +0200465 }
466
467 mbedtls_timing_set_delay( &ctx, 0, 0 );
468 busy_msleep( 200 );
469 if( mbedtls_timing_get_delay( &ctx ) != -1 )
470 FAIL;
471
472 if( verbose != 0 )
473 mbedtls_printf( "passed\n" );
474
475 if( verbose != 0 )
476 mbedtls_printf( " TIMING test #3 (hardclock / get_timer): " );
477
478 /*
479 * Allow one failure for possible counter wrapping.
480 * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
481 * since the whole test is about 10ms, it shouldn't happen twice in a row.
482 */
Jens Wiklander817466c2018-05-22 13:49:31 +0200483
484hard_test:
485 if( hardfail > 1 )
486 {
487 if( verbose != 0 )
488 mbedtls_printf( "failed (ignored)\n" );
489
490 goto hard_test_done;
491 }
492
493 /* Get a reference ratio cycles/ms */
494 millisecs = 1;
495 cycles = mbedtls_timing_hardclock();
496 busy_msleep( millisecs );
497 cycles = mbedtls_timing_hardclock() - cycles;
498 ratio = cycles / millisecs;
499
500 /* Check that the ratio is mostly constant */
501 for( millisecs = 2; millisecs <= 4; millisecs++ )
502 {
503 cycles = mbedtls_timing_hardclock();
504 busy_msleep( millisecs );
505 cycles = mbedtls_timing_hardclock() - cycles;
506
507 /* Allow variation up to 20% */
508 if( cycles / millisecs < ratio - ratio / 5 ||
509 cycles / millisecs > ratio + ratio / 5 )
510 {
511 hardfail++;
512 goto hard_test;
513 }
514 }
515
516 if( verbose != 0 )
517 mbedtls_printf( "passed\n" );
518
519hard_test_done:
520
521 if( verbose != 0 )
522 mbedtls_printf( "\n" );
523
524 return( 0 );
525}
526
527#endif /* MBEDTLS_SELF_TEST */
Jerome Forissier039e02d2022-08-09 17:10:15 +0200528#endif /* !MBEDTLS_TIMING_ALT */
Jens Wiklander817466c2018-05-22 13:49:31 +0200529#endif /* MBEDTLS_TIMING_C */