blob: eb41461320be9fd4305d8a8677966c120745edf0 [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>
59#include <sys/time.h>
60#include <signal.h>
61#include <time.h>
62
63struct _hr_time
64{
65 struct timeval start;
66};
67
68#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
367#endif /* !MBEDTLS_TIMING_ALT */
368
369#if defined(MBEDTLS_SELF_TEST)
370
371/*
372 * Busy-waits for the given number of milliseconds.
373 * Used for testing mbedtls_timing_hardclock.
374 */
375static void busy_msleep( unsigned long msec )
376{
377 struct mbedtls_timing_hr_time hires;
378 unsigned long i = 0; /* for busy-waiting */
379 volatile unsigned long j; /* to prevent optimisation */
380
381 (void) mbedtls_timing_get_timer( &hires, 1 );
382
383 while( mbedtls_timing_get_timer( &hires, 0 ) < msec )
384 i++;
385
386 j = i;
387 (void) j;
388}
389
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100390#define FAIL do \
391 { \
392 if( verbose != 0 ) \
393 { \
394 mbedtls_printf( "failed at line %d\n", __LINE__ ); \
395 mbedtls_printf( " cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d a=%lu b=%lu\n", \
396 cycles, ratio, millisecs, secs, hardfail, \
397 (unsigned long) a, (unsigned long) b ); \
398 mbedtls_printf( " elapsed(hires)=%lu elapsed(ctx)=%lu status(ctx)=%d\n", \
399 mbedtls_timing_get_timer( &hires, 0 ), \
400 mbedtls_timing_get_timer( &ctx.timer, 0 ), \
401 mbedtls_timing_get_delay( &ctx ) ); \
402 } \
403 return( 1 ); \
404 } while( 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +0200405
406/*
407 * Checkup routine
408 *
409 * Warning: this is work in progress, some tests may not be reliable enough
410 * yet! False positives may happen.
411 */
412int mbedtls_timing_self_test( int verbose )
413{
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100414 unsigned long cycles = 0, ratio = 0;
415 unsigned long millisecs = 0, secs = 0;
416 int hardfail = 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200417 struct mbedtls_timing_hr_time hires;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100418 uint32_t a = 0, b = 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200419 mbedtls_timing_delay_context ctx;
420
421 if( verbose != 0 )
422 mbedtls_printf( " TIMING tests note: will take some time!\n" );
423
Jens Wiklander817466c2018-05-22 13:49:31 +0200424 if( verbose != 0 )
425 mbedtls_printf( " TIMING test #1 (set_alarm / get_timer): " );
426
Jens Wiklander817466c2018-05-22 13:49:31 +0200427 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100428 secs = 1;
429
Jens Wiklander817466c2018-05-22 13:49:31 +0200430 (void) mbedtls_timing_get_timer( &hires, 1 );
431
432 mbedtls_set_alarm( (int) secs );
433 while( !mbedtls_timing_alarmed )
434 ;
435
436 millisecs = mbedtls_timing_get_timer( &hires, 0 );
437
438 /* For some reason on Windows it looks like alarm has an extra delay
439 * (maybe related to creating a new thread). Allow some room here. */
440 if( millisecs < 800 * secs || millisecs > 1200 * secs + 300 )
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100441 FAIL;
Jens Wiklander817466c2018-05-22 13:49:31 +0200442 }
443
444 if( verbose != 0 )
445 mbedtls_printf( "passed\n" );
446
447 if( verbose != 0 )
448 mbedtls_printf( " TIMING test #2 (set/get_delay ): " );
449
Jens Wiklander817466c2018-05-22 13:49:31 +0200450 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100451 a = 800;
452 b = 400;
453 mbedtls_timing_set_delay( &ctx, a, a + b ); /* T = 0 */
Jens Wiklander817466c2018-05-22 13:49:31 +0200454
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100455 busy_msleep( a - a / 4 ); /* T = a - a/4 */
456 if( mbedtls_timing_get_delay( &ctx ) != 0 )
457 FAIL;
Jens Wiklander817466c2018-05-22 13:49:31 +0200458
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100459 busy_msleep( a / 4 + b / 4 ); /* T = a + b/4 */
460 if( mbedtls_timing_get_delay( &ctx ) != 1 )
461 FAIL;
Jens Wiklander817466c2018-05-22 13:49:31 +0200462
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100463 busy_msleep( b ); /* T = a + b + b/4 */
464 if( mbedtls_timing_get_delay( &ctx ) != 2 )
465 FAIL;
Jens Wiklander817466c2018-05-22 13:49:31 +0200466 }
467
468 mbedtls_timing_set_delay( &ctx, 0, 0 );
469 busy_msleep( 200 );
470 if( mbedtls_timing_get_delay( &ctx ) != -1 )
471 FAIL;
472
473 if( verbose != 0 )
474 mbedtls_printf( "passed\n" );
475
476 if( verbose != 0 )
477 mbedtls_printf( " TIMING test #3 (hardclock / get_timer): " );
478
479 /*
480 * Allow one failure for possible counter wrapping.
481 * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
482 * since the whole test is about 10ms, it shouldn't happen twice in a row.
483 */
Jens Wiklander817466c2018-05-22 13:49:31 +0200484
485hard_test:
486 if( hardfail > 1 )
487 {
488 if( verbose != 0 )
489 mbedtls_printf( "failed (ignored)\n" );
490
491 goto hard_test_done;
492 }
493
494 /* Get a reference ratio cycles/ms */
495 millisecs = 1;
496 cycles = mbedtls_timing_hardclock();
497 busy_msleep( millisecs );
498 cycles = mbedtls_timing_hardclock() - cycles;
499 ratio = cycles / millisecs;
500
501 /* Check that the ratio is mostly constant */
502 for( millisecs = 2; millisecs <= 4; millisecs++ )
503 {
504 cycles = mbedtls_timing_hardclock();
505 busy_msleep( millisecs );
506 cycles = mbedtls_timing_hardclock() - cycles;
507
508 /* Allow variation up to 20% */
509 if( cycles / millisecs < ratio - ratio / 5 ||
510 cycles / millisecs > ratio + ratio / 5 )
511 {
512 hardfail++;
513 goto hard_test;
514 }
515 }
516
517 if( verbose != 0 )
518 mbedtls_printf( "passed\n" );
519
520hard_test_done:
521
522 if( verbose != 0 )
523 mbedtls_printf( "\n" );
524
525 return( 0 );
526}
527
528#endif /* MBEDTLS_SELF_TEST */
529
530#endif /* MBEDTLS_TIMING_C */