1 /*
2 * Portable interface to the CPU cycle counter
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
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.
18 */
19
20 #include "common.h"
21
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) && \
36 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
37 !defined(__HAIKU__) && !defined(__midipix__)
38 #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>
48 #include <process.h>
49
50 struct _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
63 struct _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
mbedtls_timing_hardclock(void)75 unsigned 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
mbedtls_timing_hardclock(void)92 unsigned 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
mbedtls_timing_hardclock(void)106 unsigned 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
mbedtls_timing_hardclock(void)120 unsigned 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
mbedtls_timing_hardclock(void)145 unsigned 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
mbedtls_timing_hardclock(void)160 unsigned 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
mbedtls_timing_hardclock(void)175 unsigned 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
mbedtls_timing_hardclock(void)189 unsigned 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
mbedtls_timing_hardclock(void)203 unsigned 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
217 static int hardclock_init = 0;
218 static struct timeval tv_init;
219
mbedtls_timing_hardclock(void)220 unsigned 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
236 volatile int mbedtls_timing_alarmed = 0;
237
238 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
239
mbedtls_timing_get_timer(struct mbedtls_timing_hr_time * val,int reset)240 unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
241 {
242 struct _hr_time *t = (struct _hr_time *) val;
243
244 if( reset )
245 {
246 QueryPerformanceCounter( &t->start );
247 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 }
259 }
260
261 /* It's OK to use a global because alarm() is supposed to be global anyway */
262 static DWORD alarmMs;
263
TimerProc(void * TimerContext)264 static void TimerProc( void *TimerContext )
265 {
266 (void) TimerContext;
267 Sleep( alarmMs );
268 mbedtls_timing_alarmed = 1;
269 /* _endthread will be called implicitly on return
270 * That ensures execution of thread funcition's epilogue */
271 }
272
mbedtls_set_alarm(int seconds)273 void mbedtls_set_alarm( int seconds )
274 {
275 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 }
282
283 mbedtls_timing_alarmed = 0;
284 alarmMs = seconds * 1000;
285 (void) _beginthread( TimerProc, 0, NULL );
286 }
287
288 #else /* _WIN32 && !EFIX64 && !EFI32 */
289
mbedtls_timing_get_timer(struct mbedtls_timing_hr_time * val,int reset)290 unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
291 {
292 struct _hr_time *t = (struct _hr_time *) val;
293
294 if( reset )
295 {
296 gettimeofday( &t->start, NULL );
297 return( 0 );
298 }
299 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 }
308 }
309
sighandler(int signum)310 static void sighandler( int signum )
311 {
312 mbedtls_timing_alarmed = 1;
313 signal( signum, sighandler );
314 }
315
mbedtls_set_alarm(int seconds)316 void mbedtls_set_alarm( int seconds )
317 {
318 mbedtls_timing_alarmed = 0;
319 signal( SIGALRM, sighandler );
320 alarm( seconds );
321 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 }
327 }
328
329 #endif /* _WIN32 && !EFIX64 && !EFI32 */
330
331 /*
332 * Set delays to watch
333 */
mbedtls_timing_set_delay(void * data,uint32_t int_ms,uint32_t fin_ms)334 void 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 */
mbedtls_timing_get_delay(void * data)348 int 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 */
busy_msleep(unsigned long msec)375 static 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
390 #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 )
405
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 */
mbedtls_timing_self_test(int verbose)412 int mbedtls_timing_self_test( int verbose )
413 {
414 unsigned long cycles = 0, ratio = 0;
415 unsigned long millisecs = 0, secs = 0;
416 int hardfail = 0;
417 struct mbedtls_timing_hr_time hires;
418 uint32_t a = 0, b = 0;
419 mbedtls_timing_delay_context ctx;
420
421 if( verbose != 0 )
422 mbedtls_printf( " TIMING tests note: will take some time!\n" );
423
424 if( verbose != 0 )
425 mbedtls_printf( " TIMING test #1 (set_alarm / get_timer): " );
426
427 {
428 secs = 1;
429
430 (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 )
441 FAIL;
442 }
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
450 {
451 a = 800;
452 b = 400;
453 mbedtls_timing_set_delay( &ctx, a, a + b ); /* T = 0 */
454
455 busy_msleep( a - a / 4 ); /* T = a - a/4 */
456 if( mbedtls_timing_get_delay( &ctx ) != 0 )
457 FAIL;
458
459 busy_msleep( a / 4 + b / 4 ); /* T = a + b/4 */
460 if( mbedtls_timing_get_delay( &ctx ) != 1 )
461 FAIL;
462
463 busy_msleep( b ); /* T = a + b + b/4 */
464 if( mbedtls_timing_get_delay( &ctx ) != 2 )
465 FAIL;
466 }
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 */
484
485 hard_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
520 hard_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 */
531