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 <signal.h>
60 /* time.h should be included independently of MBEDTLS_HAVE_TIME. If the
61  * platform matches the ifdefs above, it will be used. */
62 #include <time.h>
63 #include <sys/time.h>
64 struct _hr_time
65 {
66     struct timeval start;
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 
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  */
busy_msleep(unsigned long msec)374 static 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 
389 #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 )
404 
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  */
mbedtls_timing_self_test(int verbose)411 int mbedtls_timing_self_test( int verbose )
412 {
413     unsigned long cycles = 0, ratio = 0;
414     unsigned long millisecs = 0, secs = 0;
415     int hardfail = 0;
416     struct mbedtls_timing_hr_time hires;
417     uint32_t a = 0, b = 0;
418     mbedtls_timing_delay_context ctx;
419 
420     if( verbose != 0 )
421         mbedtls_printf( "  TIMING tests note: will take some time!\n" );
422 
423     if( verbose != 0 )
424         mbedtls_printf( "  TIMING test #1 (set_alarm / get_timer): " );
425 
426     {
427         secs = 1;
428 
429         (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 )
440             FAIL;
441     }
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 
449     {
450         a = 800;
451         b = 400;
452         mbedtls_timing_set_delay( &ctx, a, a + b );          /* T = 0 */
453 
454         busy_msleep( a - a / 4 );                      /* T = a - a/4 */
455         if( mbedtls_timing_get_delay( &ctx ) != 0 )
456             FAIL;
457 
458         busy_msleep( a / 4 + b / 4 );                  /* T = a + b/4 */
459         if( mbedtls_timing_get_delay( &ctx ) != 1 )
460             FAIL;
461 
462         busy_msleep( b );                          /* T = a + b + b/4 */
463         if( mbedtls_timing_get_delay( &ctx ) != 2 )
464             FAIL;
465     }
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      */
483 
484 hard_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 
519 hard_test_done:
520 
521     if( verbose != 0 )
522         mbedtls_printf( "\n" );
523 
524     return( 0 );
525 }
526 
527 #endif /* MBEDTLS_SELF_TEST */
528 #endif /* !MBEDTLS_TIMING_ALT */
529 #endif /* MBEDTLS_TIMING_C */
530