timing.c 13 KB

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