timing.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. #include "common.h"
  20. #include "mbedtls/platform.h"
  21. #if defined(MBEDTLS_TIMING_C)
  22. #include "mbedtls/timing.h"
  23. #if !defined(MBEDTLS_TIMING_ALT)
  24. #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
  25. !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
  26. !defined(__HAIKU__) && !defined(__midipix__)
  27. #error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in config.h"
  28. #endif
  29. #ifndef asm
  30. #define asm __asm
  31. #endif
  32. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  33. #include <windows.h>
  34. #include <process.h>
  35. struct _hr_time
  36. {
  37. LARGE_INTEGER start;
  38. };
  39. #else
  40. #include <unistd.h>
  41. #include <sys/types.h>
  42. #include <signal.h>
  43. /* time.h should be included independently of MBEDTLS_HAVE_TIME. If the
  44. * platform matches the ifdefs above, it will be used. */
  45. #include <time.h>
  46. #include <sys/time.h>
  47. struct _hr_time
  48. {
  49. struct timeval start;
  50. };
  51. #endif /* _WIN32 && !EFIX64 && !EFI32 */
  52. #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
  53. ( defined(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__)
  54. #define HAVE_HARDCLOCK
  55. unsigned long mbedtls_timing_hardclock( void )
  56. {
  57. unsigned long tsc;
  58. __asm rdtsc
  59. __asm mov [tsc], eax
  60. return( tsc );
  61. }
  62. #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
  63. ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
  64. /* some versions of mingw-64 have 32-bit longs even on x84_64 */
  65. #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
  66. defined(__GNUC__) && ( defined(__i386__) || ( \
  67. ( defined(__amd64__) || defined( __x86_64__) ) && __SIZEOF_LONG__ == 4 ) )
  68. #define HAVE_HARDCLOCK
  69. unsigned long mbedtls_timing_hardclock( void )
  70. {
  71. unsigned long lo, hi;
  72. asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
  73. return( lo );
  74. }
  75. #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
  76. __GNUC__ && __i386__ */
  77. #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
  78. defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) )
  79. #define HAVE_HARDCLOCK
  80. unsigned long mbedtls_timing_hardclock( void )
  81. {
  82. unsigned long lo, hi;
  83. asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
  84. return( lo | ( hi << 32 ) );
  85. }
  86. #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
  87. __GNUC__ && ( __amd64__ || __x86_64__ ) */
  88. #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
  89. defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) )
  90. #define HAVE_HARDCLOCK
  91. unsigned long mbedtls_timing_hardclock( void )
  92. {
  93. unsigned long tbl, tbu0, tbu1;
  94. do
  95. {
  96. asm volatile( "mftbu %0" : "=r" (tbu0) );
  97. asm volatile( "mftb %0" : "=r" (tbl ) );
  98. asm volatile( "mftbu %0" : "=r" (tbu1) );
  99. }
  100. while( tbu0 != tbu1 );
  101. return( tbl );
  102. }
  103. #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
  104. __GNUC__ && ( __powerpc__ || __ppc__ ) */
  105. #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
  106. defined(__GNUC__) && defined(__sparc64__)
  107. #if defined(__OpenBSD__)
  108. #warning OpenBSD does not allow access to tick register using software version instead
  109. #else
  110. #define HAVE_HARDCLOCK
  111. unsigned long mbedtls_timing_hardclock( void )
  112. {
  113. unsigned long tick;
  114. asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) );
  115. return( tick );
  116. }
  117. #endif /* __OpenBSD__ */
  118. #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
  119. __GNUC__ && __sparc64__ */
  120. #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
  121. defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
  122. #define HAVE_HARDCLOCK
  123. unsigned long mbedtls_timing_hardclock( void )
  124. {
  125. unsigned long tick;
  126. asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" );
  127. asm volatile( "mov %%g1, %0" : "=r" (tick) );
  128. return( tick );
  129. }
  130. #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
  131. __GNUC__ && __sparc__ && !__sparc64__ */
  132. #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
  133. defined(__GNUC__) && defined(__alpha__)
  134. #define HAVE_HARDCLOCK
  135. unsigned long mbedtls_timing_hardclock( void )
  136. {
  137. unsigned long cc;
  138. asm volatile( "rpcc %0" : "=r" (cc) );
  139. return( cc & 0xFFFFFFFF );
  140. }
  141. #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
  142. __GNUC__ && __alpha__ */
  143. #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
  144. defined(__GNUC__) && defined(__ia64__)
  145. #define HAVE_HARDCLOCK
  146. unsigned long mbedtls_timing_hardclock( void )
  147. {
  148. unsigned long itc;
  149. asm volatile( "mov %0 = ar.itc" : "=r" (itc) );
  150. return( itc );
  151. }
  152. #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
  153. __GNUC__ && __ia64__ */
  154. #if !defined(HAVE_HARDCLOCK) && defined(_MSC_VER) && \
  155. !defined(EFIX64) && !defined(EFI32)
  156. #define HAVE_HARDCLOCK
  157. unsigned long mbedtls_timing_hardclock( void )
  158. {
  159. LARGE_INTEGER offset;
  160. QueryPerformanceCounter( &offset );
  161. return( (unsigned long)( offset.QuadPart ) );
  162. }
  163. #endif /* !HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
  164. #if !defined(HAVE_HARDCLOCK)
  165. #define HAVE_HARDCLOCK
  166. static int hardclock_init = 0;
  167. static struct timeval tv_init;
  168. unsigned long mbedtls_timing_hardclock( void )
  169. {
  170. struct timeval tv_cur;
  171. if( hardclock_init == 0 )
  172. {
  173. gettimeofday( &tv_init, NULL );
  174. hardclock_init = 1;
  175. }
  176. gettimeofday( &tv_cur, NULL );
  177. return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
  178. + ( tv_cur.tv_usec - tv_init.tv_usec ) );
  179. }
  180. #endif /* !HAVE_HARDCLOCK */
  181. volatile int mbedtls_timing_alarmed = 0;
  182. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  183. unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
  184. {
  185. struct _hr_time *t = (struct _hr_time *) val;
  186. if( reset )
  187. {
  188. QueryPerformanceCounter( &t->start );
  189. return( 0 );
  190. }
  191. else
  192. {
  193. unsigned long delta;
  194. LARGE_INTEGER now, hfreq;
  195. QueryPerformanceCounter( &now );
  196. QueryPerformanceFrequency( &hfreq );
  197. delta = (unsigned long)( ( now.QuadPart - t->start.QuadPart ) * 1000ul
  198. / hfreq.QuadPart );
  199. return( delta );
  200. }
  201. }
  202. /* It's OK to use a global because alarm() is supposed to be global anyway */
  203. static DWORD alarmMs;
  204. static void TimerProc( void *TimerContext )
  205. {
  206. (void) TimerContext;
  207. Sleep( alarmMs );
  208. mbedtls_timing_alarmed = 1;
  209. /* _endthread will be called implicitly on return
  210. * That ensures execution of thread function's epilogue */
  211. }
  212. void mbedtls_set_alarm( int seconds )
  213. {
  214. if( seconds == 0 )
  215. {
  216. /* No need to create a thread for this simple case.
  217. * Also, this shorcut is more reliable at least on MinGW32 */
  218. mbedtls_timing_alarmed = 1;
  219. return;
  220. }
  221. mbedtls_timing_alarmed = 0;
  222. alarmMs = seconds * 1000;
  223. (void) _beginthread( TimerProc, 0, NULL );
  224. }
  225. #else /* _WIN32 && !EFIX64 && !EFI32 */
  226. unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset )
  227. {
  228. struct _hr_time *t = (struct _hr_time *) val;
  229. if( reset )
  230. {
  231. gettimeofday( &t->start, NULL );
  232. return( 0 );
  233. }
  234. else
  235. {
  236. unsigned long delta;
  237. struct timeval now;
  238. gettimeofday( &now, NULL );
  239. delta = ( now.tv_sec - t->start.tv_sec ) * 1000ul
  240. + ( now.tv_usec - t->start.tv_usec ) / 1000;
  241. return( delta );
  242. }
  243. }
  244. static void sighandler( int signum )
  245. {
  246. mbedtls_timing_alarmed = 1;
  247. signal( signum, sighandler );
  248. }
  249. void mbedtls_set_alarm( int seconds )
  250. {
  251. mbedtls_timing_alarmed = 0;
  252. signal( SIGALRM, sighandler );
  253. alarm( seconds );
  254. if( seconds == 0 )
  255. {
  256. /* alarm(0) cancelled any previous pending alarm, but the
  257. handler won't fire, so raise the flag straight away. */
  258. mbedtls_timing_alarmed = 1;
  259. }
  260. }
  261. #endif /* _WIN32 && !EFIX64 && !EFI32 */
  262. /*
  263. * Set delays to watch
  264. */
  265. void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms )
  266. {
  267. mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
  268. ctx->int_ms = int_ms;
  269. ctx->fin_ms = fin_ms;
  270. if( fin_ms != 0 )
  271. (void) mbedtls_timing_get_timer( &ctx->timer, 1 );
  272. }
  273. /*
  274. * Get number of delays expired
  275. */
  276. int mbedtls_timing_get_delay( void *data )
  277. {
  278. mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
  279. unsigned long elapsed_ms;
  280. if( ctx->fin_ms == 0 )
  281. return( -1 );
  282. elapsed_ms = mbedtls_timing_get_timer( &ctx->timer, 0 );
  283. if( elapsed_ms >= ctx->fin_ms )
  284. return( 2 );
  285. if( elapsed_ms >= ctx->int_ms )
  286. return( 1 );
  287. return( 0 );
  288. }
  289. #if defined(MBEDTLS_SELF_TEST)
  290. /*
  291. * Busy-waits for the given number of milliseconds.
  292. * Used for testing mbedtls_timing_hardclock.
  293. */
  294. static void busy_msleep( unsigned long msec )
  295. {
  296. struct mbedtls_timing_hr_time hires;
  297. unsigned long i = 0; /* for busy-waiting */
  298. volatile unsigned long j; /* to prevent optimisation */
  299. (void) mbedtls_timing_get_timer( &hires, 1 );
  300. while( mbedtls_timing_get_timer( &hires, 0 ) < msec )
  301. i++;
  302. j = i;
  303. (void) j;
  304. }
  305. #define FAIL do \
  306. { \
  307. if( verbose != 0 ) \
  308. { \
  309. mbedtls_printf( "failed at line %d\n", __LINE__ ); \
  310. mbedtls_printf( " cycles=%lu ratio=%lu millisecs=%lu secs=%lu hardfail=%d a=%lu b=%lu\n", \
  311. cycles, ratio, millisecs, secs, hardfail, \
  312. (unsigned long) a, (unsigned long) b ); \
  313. mbedtls_printf( " elapsed(hires)=%lu elapsed(ctx)=%lu status(ctx)=%d\n", \
  314. mbedtls_timing_get_timer( &hires, 0 ), \
  315. mbedtls_timing_get_timer( &ctx.timer, 0 ), \
  316. mbedtls_timing_get_delay( &ctx ) ); \
  317. } \
  318. return( 1 ); \
  319. } while( 0 )
  320. /*
  321. * Checkup routine
  322. *
  323. * Warning: this is work in progress, some tests may not be reliable enough
  324. * yet! False positives may happen.
  325. */
  326. int mbedtls_timing_self_test( int verbose )
  327. {
  328. unsigned long cycles = 0, ratio = 0;
  329. unsigned long millisecs = 0, secs = 0;
  330. int hardfail = 0;
  331. struct mbedtls_timing_hr_time hires;
  332. uint32_t a = 0, b = 0;
  333. mbedtls_timing_delay_context ctx;
  334. if( verbose != 0 )
  335. mbedtls_printf( " TIMING tests note: will take some time!\n" );
  336. if( verbose != 0 )
  337. mbedtls_printf( " TIMING test #1 (set_alarm / get_timer): " );
  338. {
  339. secs = 1;
  340. (void) mbedtls_timing_get_timer( &hires, 1 );
  341. mbedtls_set_alarm( (int) secs );
  342. while( !mbedtls_timing_alarmed )
  343. ;
  344. millisecs = mbedtls_timing_get_timer( &hires, 0 );
  345. /* For some reason on Windows it looks like alarm has an extra delay
  346. * (maybe related to creating a new thread). Allow some room here. */
  347. if( millisecs < 800 * secs || millisecs > 1200 * secs + 300 )
  348. FAIL;
  349. }
  350. if( verbose != 0 )
  351. mbedtls_printf( "passed\n" );
  352. if( verbose != 0 )
  353. mbedtls_printf( " TIMING test #2 (set/get_delay ): " );
  354. {
  355. a = 800;
  356. b = 400;
  357. mbedtls_timing_set_delay( &ctx, a, a + b ); /* T = 0 */
  358. busy_msleep( a - a / 4 ); /* T = a - a/4 */
  359. if( mbedtls_timing_get_delay( &ctx ) != 0 )
  360. FAIL;
  361. busy_msleep( a / 4 + b / 4 ); /* T = a + b/4 */
  362. if( mbedtls_timing_get_delay( &ctx ) != 1 )
  363. FAIL;
  364. busy_msleep( b ); /* T = a + b + b/4 */
  365. if( mbedtls_timing_get_delay( &ctx ) != 2 )
  366. FAIL;
  367. }
  368. mbedtls_timing_set_delay( &ctx, 0, 0 );
  369. busy_msleep( 200 );
  370. if( mbedtls_timing_get_delay( &ctx ) != -1 )
  371. FAIL;
  372. if( verbose != 0 )
  373. mbedtls_printf( "passed\n" );
  374. if( verbose != 0 )
  375. mbedtls_printf( " TIMING test #3 (hardclock / get_timer): " );
  376. /*
  377. * Allow one failure for possible counter wrapping.
  378. * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
  379. * since the whole test is about 10ms, it shouldn't happen twice in a row.
  380. */
  381. hard_test:
  382. if( hardfail > 1 )
  383. {
  384. if( verbose != 0 )
  385. mbedtls_printf( "failed (ignored)\n" );
  386. goto hard_test_done;
  387. }
  388. /* Get a reference ratio cycles/ms */
  389. millisecs = 1;
  390. cycles = mbedtls_timing_hardclock();
  391. busy_msleep( millisecs );
  392. cycles = mbedtls_timing_hardclock() - cycles;
  393. ratio = cycles / millisecs;
  394. /* Check that the ratio is mostly constant */
  395. for( millisecs = 2; millisecs <= 4; millisecs++ )
  396. {
  397. cycles = mbedtls_timing_hardclock();
  398. busy_msleep( millisecs );
  399. cycles = mbedtls_timing_hardclock() - cycles;
  400. /* Allow variation up to 20% */
  401. if( cycles / millisecs < ratio - ratio / 5 ||
  402. cycles / millisecs > ratio + ratio / 5 )
  403. {
  404. hardfail++;
  405. goto hard_test;
  406. }
  407. }
  408. if( verbose != 0 )
  409. mbedtls_printf( "passed\n" );
  410. hard_test_done:
  411. if( verbose != 0 )
  412. mbedtls_printf( "\n" );
  413. return( 0 );
  414. }
  415. #endif /* MBEDTLS_SELF_TEST */
  416. #endif /* !MBEDTLS_TIMING_ALT */
  417. #endif /* MBEDTLS_TIMING_C */