timing.c 14 KB

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